使用phonegap API从Android中的sqlite检索数据时出现INVALID_ACCESS_ERR

发布于 2025-01-02 01:24:44 字数 2972 浏览 1 评论 0原文

这是我的 Javascript 代码

 document.addEventListener("deviceready", onDeviceReady, false);

// Populate the database
//
function populateDB(tx) {
    alert("populateDB");
    tx.executeSql('DROP TABLE IF EXISTS DEMO');
    tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
    tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
    tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
}

// Query the database
//
function queryDB(tx) {
    alert("queryDB");
    tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
}

// Query the success callback
//
function querySuccess(tx, results) {
    alert("querySuccess");
    // this will be empty since no rows were inserted.
    console.log("Insert ID = " + results.insertId);
    // this will be 0 since it is a select statement
    console.log("Rows Affected = " + results.rowAffected);
    // the number of rows returned by the select statement
    console.log("Insert ID = " + results.rows.length);
}

// Transaction error callback
//
function errorCB(err) {
    alert("errorCB");
    console.log("Error processing SQL: "+err.code);
}

// Transaction success callback
//
function successCB() {
    alert("successCB");
    var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
    db.transaction(queryDB, errorCB);
}

// PhoneGap is ready
//
function onDeviceReady() {
    alert("onDeviceReady");
    var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
    db.transaction(populateDB, errorCB, successCB);
}

,但我收到此错误日志,

D/DroidGap(1674): DroidGap.loadUrl(file:///android_asset/www/index.html)
D/DroidGap(1674): DroidGap: url=file:///android_asset/www/index.html   baseUrl=file:///android_asset/www/
D/DroidGap(1674): DroidGap.init()
D/SoftKeyboardDetect(1674): Ignore this event
D/SoftKeyboardDetect(1674): Ignore this event
D/dalvikvm(1674): GC_FOR_MALLOC freed 1758 objects / 121720 bytes in 172ms
I/Database(1674): sqlite returned: error code = 14, msg = cannot open file at source   line 25467
D/PhoneGapLog(1674): DroidGap:  onExceededDatabaseQuota estimatedSize: 200000    currentQuota: 0  totalUsedQuota: 0
D/PhoneGapLog(1674): calling quotaUpdater.updateQuota newQuota: 200000
D/PhoneGapLog(1674): INVALID_ACCESS_ERR: DOM Exception 15: A parameter or an operation  was not supported by the underlying object.
D/PhoneGapLog(1674): file:///android_asset/www/index.html: Line 232 :  INVALID_ACCESS_ERR: DOM Exception 15: A parameter or an operation was not supported by the    underlying object.
E/Web Console(1674): INVALID_ACCESS_ERR: DOM Exception 15: A parameter or an operation was not supported by the underlying object. at file:///android_asset/www/index.html:232
D/PhoneGapLog(1674): Error processing SQL: 0
D/PhoneGapLog(1674): file:///android_asset/www/index.html: Line 243 : Error processing SQL: 0

我正在使用 Phonegap 版本 1.4.0。 这里有什么问题吗?

This is my Javascript code

 document.addEventListener("deviceready", onDeviceReady, false);

// Populate the database
//
function populateDB(tx) {
    alert("populateDB");
    tx.executeSql('DROP TABLE IF EXISTS DEMO');
    tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
    tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
    tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
}

// Query the database
//
function queryDB(tx) {
    alert("queryDB");
    tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
}

// Query the success callback
//
function querySuccess(tx, results) {
    alert("querySuccess");
    // this will be empty since no rows were inserted.
    console.log("Insert ID = " + results.insertId);
    // this will be 0 since it is a select statement
    console.log("Rows Affected = " + results.rowAffected);
    // the number of rows returned by the select statement
    console.log("Insert ID = " + results.rows.length);
}

// Transaction error callback
//
function errorCB(err) {
    alert("errorCB");
    console.log("Error processing SQL: "+err.code);
}

// Transaction success callback
//
function successCB() {
    alert("successCB");
    var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
    db.transaction(queryDB, errorCB);
}

// PhoneGap is ready
//
function onDeviceReady() {
    alert("onDeviceReady");
    var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
    db.transaction(populateDB, errorCB, successCB);
}

But i am getting this error log

D/DroidGap(1674): DroidGap.loadUrl(file:///android_asset/www/index.html)
D/DroidGap(1674): DroidGap: url=file:///android_asset/www/index.html   baseUrl=file:///android_asset/www/
D/DroidGap(1674): DroidGap.init()
D/SoftKeyboardDetect(1674): Ignore this event
D/SoftKeyboardDetect(1674): Ignore this event
D/dalvikvm(1674): GC_FOR_MALLOC freed 1758 objects / 121720 bytes in 172ms
I/Database(1674): sqlite returned: error code = 14, msg = cannot open file at source   line 25467
D/PhoneGapLog(1674): DroidGap:  onExceededDatabaseQuota estimatedSize: 200000    currentQuota: 0  totalUsedQuota: 0
D/PhoneGapLog(1674): calling quotaUpdater.updateQuota newQuota: 200000
D/PhoneGapLog(1674): INVALID_ACCESS_ERR: DOM Exception 15: A parameter or an operation  was not supported by the underlying object.
D/PhoneGapLog(1674): file:///android_asset/www/index.html: Line 232 :  INVALID_ACCESS_ERR: DOM Exception 15: A parameter or an operation was not supported by the    underlying object.
E/Web Console(1674): INVALID_ACCESS_ERR: DOM Exception 15: A parameter or an operation was not supported by the underlying object. at file:///android_asset/www/index.html:232
D/PhoneGapLog(1674): Error processing SQL: 0
D/PhoneGapLog(1674): file:///android_asset/www/index.html: Line 243 : Error processing SQL: 0

I am using Phonegap version 1.4.0.
Whats the problem here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝海 2025-01-09 01:24:44

不知道您是否已经解决了这个问题,但我使用了相同的示例代码,也遇到了同样的问题。这个例子是不正确的。 Phonegap 现在有一个更新的示例。将您的查询方法替换为

function querySuccess(tx, results) {
    var len = results.rows.length;
    console.log("DEMO table: " + len + " rows found.");
    for (var i=0; i<len; i++){
        console.log("Row = " + i + " ID = " + results.rows.item(i).id + " Data =  " + results.rows.item(i).data);
    }
}

Don't know if you already fixed this, but I was using your same example code, with the same problem. The example was incorrect. Phonegap now has an updated example. Replace your query method with

function querySuccess(tx, results) {
    var len = results.rows.length;
    console.log("DEMO table: " + len + " rows found.");
    for (var i=0; i<len; i++){
        console.log("Row = " + i + " ID = " + results.rows.item(i).id + " Data =  " + results.rows.item(i).data);
    }
}
夜夜流光相皎洁 2025-01-09 01:24:44

根据 Web SQL 规范

insertId 属性必须返回插入的行的行 ID
SQLResultSet对象的SQL语句插入到数据库中,如果
语句插入一行。如果该语句插入了多行,则
最后一行的 ID 必须是返回的 ID。 如果声明没有
插入一行,那么该属性必须改为引发
INVALID_ACCESS_ERR 异常。

您的 queryDB 方法执行 SELECT。因此,根据定义,它不能插入任何行。因此,insertId 在此上下文中无效。

According to the Web SQL spec:

The insertId attribute must return the row ID of the row that the
SQLResultSet object's SQL statement inserted into the database, if the
statement inserted a row. If the statement inserted multiple rows, the
ID of the last row must be the one returned. If the statement did not
insert a row, then the attribute must instead raise an
INVALID_ACCESS_ERR exception.

Your queryDB method does a SELECT. So, by definition, it can't insert any rows. Therefore, insertId is not valid in this context.

三生池水覆流年 2025-01-09 01:24:44
try {
console.log("Insert ID = " + results.insertId);
} catch(exc) {
// console.log(exc)
console.log("Insert ID = " + null);
}
try {
console.log("Insert ID = " + results.insertId);
} catch(exc) {
// console.log(exc)
console.log("Insert ID = " + null);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文