简体   繁体   中英

JavaScript WebSQL databases

I'm having a problem with my code below.

db = openDatabase('mydb', '1.0', 'my first database', 5 * 1024 * 1024);

if(!db) {   alert("Failed to connect to database.");    }

else {

function doQuery(queryType,query) {

function errorHandler(tr, error) {
alert('Oops.  Error was: '+error.message+' (Code '+error.code+')');
var we_think_this_error_is_fatal = true;
if (we_think_this_error_is_fatal) return true;
return false;
}

if(queryType == "SELECT") {
function dataHandler(tr, rs) {alert("1")}
db.transaction(function (tr) {  tr.executeSql(query,[],dataHandler,errorHandler)})
}

else { 
function dataHandler(tr, rs) { alert("2")}
db.transaction(function (tr) {  tr.executeSql(query,[],dataHandler,errorHandler)})
}

} // doquery
} // else

db, tables, and records already was inserted, so, then, when im calling it in mypage:

<td align="right">
<div id="body">
<textarea id="txt" rows="20" cols="48">
</textarea>
</div>
<input type="button" id="button" onClick="doQuery('SELECT',txtarea.value)" value="Execute SQL">
</td>

I'm getting alert(2) instead of alert(1), why?

txtarea.value of course, is textarea object, so whats wrong?

I would take all the function definitions out of the first else block (get rid of that else). Change the name of the second dataHandler function to dataHandler2 and see what you get.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM