我这里有点问题。我尝试从数据库表中获取最后一个id,但它给了我一个错误
the statement callback raised an exception or statement error callback did not return false
代码在这里:
function onDeviceReady(){
var db=window.openDatabase("test", "1.0", "Test", 2000000);
db.transaction(creare_baza, eroaredb, succesdb);
//alert("intrat");
}
function creare_baza(tx){
tx.executeSql('DROP TABLE IF EXISTS demo');
tx.executeSql("CREATE TABLE IF NOT EXISTS demo (id unique, info)");
var ult_id=0;
tx.executeSql("SELECT * FROM demo", [], ult_id=function(tx, rez){
return rez.insertId;
}, eroaredb);
tx.executeSql("INSERT INTO demo(id, info) VALUES("+(parseInt(ult_id)+1)+", 'ceva')");
}
function cere(tx){
tx.executeSql("SELECT * FROM demo", [], onsucces, eroaredb);
}
function onsucces(tx, rezultate){
var html="Numar de randuri = "+rezultate.rows.length;
for(var i=0;i<rezultate.rows.length;i++){
html=html+"Randul "+i+" cu id="+rezultate.rows.item(i).id+" si info="+rezultate.rows.item(i).info;
}
alert(html);
}
function eroaredb(err) {
alert("Eroare la procesare: "+err.message);
}
function succesdb() {
var db=window.openDatabase("test", "1.0", "Test", 2000000);
db.transaction(cere, eroaredb);
}
另一个问题是如何将新参数发送到插入新行的函数?像:
function new_row(tx, id, data){
tx.executeSql("INSERT INTO demo (id, info) VALUES("+id+", '"+data+"')");
}
答案 0 :(得分:0)
回答第二个问题,
SQLiteDatabase db=openOrCreateDatabase("your_DB_NAME", MODE_PRIVATE, null);
String s="INSERT INTO demo VALUES ('"+id+"','"+data+"');";
db.execSQL(s);
db.close();
以上代码对我有用。