我的代码有三个具体问题,在代码底部进行了评论。此外,我的浏览器的控制台显示数据库打开,等待事务和打开。
OPEN database: myapp.db
CT: registering tables
new transaction is waiting for open operation
DB opened: myapp.db
Avar: 1
CT: CHECKING myapp table
Bvar: 1
A1 Success: {"rows":{"length":0},"rowsAffected":0}
B1 Success: , {"rows":{"length":0},"rowsAffected":0}
Error processing SQL: 0, a statement error callback did not return false: no such table: myInfo (code 1): ,
while compiling: SELECT * FROM myInfo
#################################################################
Error Code : 1 (SQLITE_ERROR) Caused By : SQL(query) error or missing database. (no such table: myInfo (code 1): , while compiling: SELECT
* FROM myInfo)
#################################################################
OPEN database: myapp.db
CT: registering tables
new transaction is waiting for open operation
DB opened: myapp.db
Avar: 1
CT: CHECKING myapp table
Bvar: 1
A1 Success: {"rows":{"length":0},"rowsAffected":0}
B1 Success: , {"rows":{"length":0},"rowsAffected":0}
Error processing SQL: 0, a statement error callback did not return false: no such table: myInfo (code 1): ,
while compiling: SELECT * FROM myInfo
#################################################################
Error Code : 1 (SQLITE_ERROR) Caused By : SQL(query) error or missing database. (no such table: myInfo (code 1): , while compiling: SELECT
* FROM myInfo)
#################################################################
function isRegistered() {
var db = window.sqlitePlugin.openDatabase({name: "my.db"});
console.log("CT: registering tables") ;
db.transaction(checkTable, errorCB,function(){
console.log("Success") ;
});
}
function isTableExists(tx, tableName, callback) {
tx.executeSql('SELECT * FROM '+tableName, [], function(tx, resultSet) {
if (resultSet.rows.length <= 0) {
callback(false,0);
} else {
callback(true,2);
}
}, function(err) {
callback(false,1);
})
};
function checkTable(tx) {
// create table
isTableExists(tx, "myInfo", function(status,avar) {
console.log("Avar: "+avar) ;
if (!status) {
console.log("CT: CHECKING myInfo") ;
tx.executeSql("CREATE TABLE IF NOT EXISTS myInfo ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,'name' VARCHAR,'phone' VARCHAR,'email' VARCHAR,'fbID' VARCHAR,'fbToken' VARCHAR,'regCode' VARCHAR)",[],function(tx,res) {
console.log("A1 Success: "+JSON.stringify(res)) ;
tx.executeSql('INSERT INTO myInfo VALUES (?,?,?,?,?,?,?)',[0,"","","","","",""],function(res) {
console.log("A2 Success: " + JSON.stringify(res) + " -- probably 1");
}, function(err) {
console.log("A2 fail: "+JSON.stringify(err)) ;
}) ;
},function(err) {
console.log("A1 Fail : "+ JSON.stringify(err)) ;
}) ;
}
}, function(err){
console.log("A Fail: "+JSON.stringify(err)) ;
}) ;
isTableExists(tx, "mySettings", function(status,bvar) {
console.log("Bvar: "+bvar) ;
if (!status) {
tx.executeSql('CREATE TABLE IF NOT EXISTS "mySettings" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,"vendor" VARCHAR,"display" VARCHAR,"aggregate" VARCHAR)',[],function(tx,res) {
console.log("B1 Success: , "+ JSON.stringify(res)) ;
var vendors = ["General","Twist","Unter","Sobi"] ;
for (var n=0;n<vendors.length;n++) {
tx.executeSql('INSERT INTO mySettings (id,vendor,display,aggregate) VALUES (?,?,?,?);',["","","",""],function(res) {
console.log("B2 Success: " + n + " : " + JSON.stringify(res)) ;
},function(err) {
console.log("B2 Fail: "+JSON.stringify(err)) ;
}) ;
}
},function(err) {
console.log("B1 Fail: "+JSON.stringify(err)) ;
}) ;
}
}, function(err){
console.log("B Fail: "+JSON.stringify(err)) ;
}) ;
}
/* three issues:
1. everytime I execute the app, it "creates" the tables...it should only do it once. Avar & Bvar are returning "1" every time
2. The next tx.executeSql("INSERT...is not triggering (that I can tell).
3. Getting general error from isRegistered(...errorCB...); - which I believe is coming from function isTableExists()
Error processing SQL: 0, a statement error callback did not return false: no such table: myInfo (code 1): , while compiling: SELECT * FROM myInfo
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(no such table: myInfo (code 1): , while compiling: SELECT * FROM myInfo)
#################################################################
*/
答案 0 :(得分:0)
尝试使用同步事务 db.executeSql 或 db.sqlBatch (请参阅。https://www.npmjs.com/package/cordova-sqlite-storage)。
function isRegistered() {
var db = window.sqlitePlugin.openDatabase({name: "my.db"});
populateDB(db);
}
function isTableExists(db, tableName, callback) {
db.executeSql('SELECT * FROM ' + tableName, [], function (res) {
callback(true);
}, function (err) {
callback(false);
});
}
function populateDB(db) {
isTableExists(db, "mySettings", function (status) {
if (!status) {
db.sqlBatch([
'CREATE TABLE IF NOT EXISTS "mySettings" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,"vendor" VARCHAR,"display" VARCHAR,"aggregate" VARCHAR)',
['INSERT INTO mySettings (id,vendor,display,aggregate) VALUES (?,?,?,?)', ["","","",""]]
], function () {
}, function (error) {
console.log('Populate table error: ' + error.message);
});
}
});
}