我是Sqlite的新手,我无法在DB中插入值,通过以下代码创建数据库和表,但是没有插入值,我的代码有什么问题?有什么建议!!!我能够看到警报值已存储但无法在表数据中看到。
$("#btnDefaultSave").click(function(){
var Latlongs = "Test1";
var Location = "Test2";
var Category = "Test3";
var Address = "Test4";
var db = window.sqlitePlugin.openDatabase({name: "TestDB"});
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS Locationlog (id INTEGER PRIMARY KEY, Coordinates TEXT, Location TEXT, Category TEXT, Address TEXT)');
});
db.transaction(function (tx){
tx.executeSql('INSERT INTO Locationlog (Coordinates, Location, Category, Address) VALUES ('+Latlongs+', '+Location+', '+Category+', '+Address+')');
alert("values are stored");
});
});
答案 0 :(得分:0)
我已经更改了插入查询的格式,如下所示,
db.transaction(function (tx){
tx.executeSql('INSERT INTO Locationlog (Coordinates, Location, Category, Address) VALUES (?,?,?,?)',[Latlongs,Location,Category,Address]);
alert("values are stored");
});
答案 1 :(得分:0)
在您的情况下,当显示“存储值”消息时,“insert ..”SQL语句可能仍在执行。请检查db事务是否在成功回调中完成。
db.transaction(function (tx) {
tx.executeSql('INSERT INTO Locationlog (Coordinates, Location, Category, Address) VALUES (?,?,?,?)',[Latlongs,Location,Category,Address]);
}, function successCB() {
alert("values are stored!");
}, function errorCB() {
alert("Error!");
}
);