从phoneGap从SQlite数据库检索值时出错

Error in retrieving values from SQlite DB from phoneGap

本文关键字:出错 数据库 phoneGap SQlite 检索      更新时间:2023-09-26
function populateDB(tx)
{
    tx.executeSql('DROP TABLE IF EXISTS test');
    tx.executeSql('CREATE TABLE IF NOT EXISTS test(course TEXT, grade INTEGER)');
    tx.executeSql('INSERT INTO test(course,grade) VALUES ("Geography, World",90)');
    tx.executeSql('INSERT INTO test(course,grade) VALUES ("Health",92)');
    tx.executeSql('INSERT INTO test(course,grade) VALUES ("Literature, English",91)');
    tx.executeSql('INSERT INTO test(course,grade) VALUES ("Math 201",85)');
    tx.executeSql('INSERT INTO test(course,grade) VALUES ("Science 202",95)');
}
function queryDB(tx)
{
    tx.executeSql('SELECT * FROM test',[],querrySuccess,errorCB);
}
function querrySuccess(tx,results)
{
    var len = results.rows.length;
    alert("test TABLE: "+ len +"row(s) found");
    for(var i=0;i<n;i++)
    {
        console.log("Row = " + i + " COURSE = " + results.rows.item(i).course + " GRADE =  " + results.rows.item(i).grade);
    }
}
function errorCB(err)
{
    alert("Error processing SQL"+err.code);
}
function successCB()
{
    alert("Success!");
}
function create_database()
{
    var db = window.openDatabase("Database","1.0", "Demo DB", 200000);
    db.transaction(populateDB,errorCB,successCB);
    db.transaction(queryDB,errorCB);
}
在上面的代码中,我创建了一个名为test的新数据库。当我的HTML表单加载时,函数create_database()被调用。我的问题是,它显示了显示行数的警报,但随后它给出了一个警报"错误处理SQL",即它被重定向到errorCB函数。Y ?我没有做错什么吗?提前感谢

我将更仔细地查看您的代码,但首先您的表中没有任何主键。也许你可以这样测试:

"CREATE TABLE IF NOT EXISTS test(ID INTEGER PRIMARY KEY AUTOINCREMENT, course TEXT, grade INTEGER)"
use in callback function like this.
tx.executeSql("CREATE TABLE IF NOT EXISTS test(ID INTEGER PRIMARY KEY AUTOINCREMENT, course TEXT, grade INTEGER)",[], function (tx, results) {
       tx.executeSql('INSERT INTO test(course,grade) VALUES ("Geography, World",90)');
       tx.executeSql('INSERT INTO test(course,grade) VALUES ("Health",92)');
       tx.executeSql('INSERT INTO test(course,grade) VALUES ("Literature, English",91)');
       tx.executeSql('INSERT INTO test(course,grade) VALUES ("Math 201",85)');
       tx.executeSql('INSERT INTO test(course,grade) VALUES ("Science 202",95)');
       tx.executeSql('SELECT * FROM test',[],function(result){

       },function(error) {
           alert("erorr..." + error);
       });
});

这是因为db.transaction调用是异步的。在创建表和插入数据的前一个事务完成之前,试图从数据库中读取数据。您应该在第一个事务完成后启动第二个事务