我有一个textfield deviceid我试图将该值存储到sqlite数据库,然后尝试以其他形式检索它但我得到了下面的错误。这是正常工作,当我在桌面上运行它但我得到了我尝试在phonegap app中运行时出错。
错误处理SQL:语句回调引发异常或语句errorcallback未返回false New.html:
<script type="text/javascript"> document.addEventListener("deviceready", onDeviceReady, false);
var db,did;
function onDeviceReady() {
db = window.openDatabase("Watersenz", "1.0", "Cordova Demo", 2*1024*1024);
db.transaction(createDB, errorCB, successCB);
}
function createDB(tx) {
alert("createDB");
tx.executeSql('DROP TABLE IF EXISTS USERIDDETAILS');
tx.executeSql('CREATE TABLE IF NOT EXISTS USERIDDETAILS (deviceid TEXT NOT NULL)');
}
function errorCB(err) {
alert("Error processing SQL: "+err.code);
console.log("Error processing SQL: "+err.message);
}
function successCB() {
alert("success");
console.log("success");
}
function insertDB(tx) {
did = $("[name='deviceid']").val();
var sql='INSERT INTO USERIDDETAILS(deviceid)VALUES(?)';
tx.executeSql(sql,[did],sucessQueryDB,errorCB);
console.log(did);
alert(did);
}
function sucessQueryDB(tx){
alert("selected");
tx.executeSql('SELECT * FROM USERIDDETAILS',[],renderList,errorCB);
}
function renderList(tx,results){
alert("rendering");
var htmlstring='';
var len=results.rows.length;
alert("rendered called"+ len);
for(var i=0;i<len;i++){
htmlstring += '<li>'+ results.rows.item(i).deviceid+'</li>';
}
$('#resultList').html(htmlstring);
$('#resultList').listview('refresh');
}
function onClick(){
db.transaction(insertDB,errorCB);
window.open("new1.html");
return false;
}</script>
</head>
<body>
<div id="header">
<h3> Login </h3>
</div>
<div id="section">
<form id="myForm">
<br><input type="text" class="one" name="deviceid" id="deviceid" placeholder="DeviceId"><br>
<input type="button" class="button" value="SUBMIT" onclick="return onClick()">
</form>
</div>
New1.html:
script type="text/javascript">
var db;
function loaddb()
{
db = window.openDatabase("Watersenz", "1.0", "Cordova Demo", 2*1024*1024);
db.transaction(successCB,errorCB);
console.log("loaded");
alert("loaded");
}
function errorCB(err) {
alert("Error processing SQL: "+err.code);
alert("Error processing SQL: "+err.message);
console.log("Error processing SQL: "+err.message);
}
function successCB() {
console.log("success");
alert("success");
}
function queryDB(tx){
tx.executeSql('SELECT * FROM USERIDDETAILS',[],renderList1,errorCB);
console.log("selected");
alert("selected");
}
function repopulateId(){
db.transaction(queryDB,errorCB);
console.log("populated");
alert("populated");
}
function renderList1(tx,results){
console.log("rendering");
alert("rendering");
var htmlstring='';
var len=results.rows.length;
var did = results.rows.item(0).deviceid;
$('#deviceid').val(did);
}</head>
<body>
<div id="header">
<h3> User Profile </h3>
</div>
<div>
<script type="text/javascript">
$(document).ready(function() {
loaddb();
repopulateId();
});
</script>
</div>
<div id="section">
<form method="post" id="myForm">
Device Id:<br><input type="text" class="one" name="deviceid" id="deviceid">
<ul data-role="listview" id="resultList">
</ul>
</form>
</div>
</body>
任何人都可以帮我解决问题所在.Thankyou。
答案 0 :(得分:0)
由于sqlite调用是异步的,执行不会等待查询返回数据。
$(document).ready(function() {
loaddb();
//repopulateId();
});
在 loaddb()功能的成功回调中调用 repopulateId()。
function successCB() {
console.log("success");
alert("success");
repopulateId();
}
希望这会有所帮助