2012-02-03 42 views
1

Android中檢索從sqlite的數據這是我的Javascript代碼INVALID_ACCESS_ERR同時使用PhoneGap的API

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); 
} 

但我我使用PhoneGap的版本1.4.0收到此錯誤日誌

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 

。 這裏有什麼問題?

回答

1

按照Web SQL spec

的insertId屬性必須返回該 SQLResultSet對象的SQL語句插入到數據庫中,如果 語句插入行的行的行ID。如果語句插入多行,則最後一行的ID必須是返回的ID。 如果語句不是 插入一行,則該屬性必須改爲產生一個 INVALID_ACCESS_ERR異常。

您的queryDB方法執行SELECT操作。所以,根據定義,它不能插入任何行。因此,insertId在此上下文中無效。

3

不知道你是否已經解決了這個問題,但是我使用了相同的示例代碼,並且遇到了同樣的問題。該示例不正確。 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); 
    } 
} 
0
try { 
console.log("Insert ID = " + results.insertId); 
} catch(exc) { 
// console.log(exc) 
console.log("Insert ID = " + null); 
} 
+0

更換您的查詢方法形容,以及它將如何幫助用戶解決他的問題和回答之前,務必閱讀常見問題解答 – Hamad 2014-01-11 10:13:21