2013-11-21 60 views
0

我試圖插入數據到websql中。我正在Chrome上進行測試,但無法正常工作。我得到error processing: SQL undefinedphonegap/cordova上的websql錯誤

下面是代碼:

$(document).ready(function() { 

var db = window.openDatabase("Database", "1.0", "News", 200000); 
db.transaction(populateDB, errorCB, successCB); 

//create table and insert some record 
function populateDB(tx) 
{ 
    tx.executeSql('CREATE TABLE IF NOT EXISTS news (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, description TEXT NOT NULL,link TEXT NOT NULL,category INT)'); 

    tx.executeSql('INSERT INTO news (title,descriptionlink,category) VALUES (?,?,?,?)',["test1","descrip","link2",2]); 
} 


// Transaction error callback 
// 
function errorCB(tx, err) { 
    alert("Error processing SQL: "+err); 
} 

// Transaction success callback 
// 
function successCB() { 
    alert("success!"); 
}}); 

回答

4

用的PhoneGap和科爾多瓦,你應該使用deviceready事件做你的東西。而不是在「文檔準備好」事件。

確保在這些代碼之前包含cordova.js文件。

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

function onDeviceReady() { 
    var db = window.openDatabase("Database", "1.0", "News", 200000); 
    db.transaction(populateDB, errorCB, successCB); 
} 

//create table and insert some record 
function populateDB(tx) 
{ 
    tx.executeSql('CREATE TABLE IF NOT EXISTS news (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, description TEXT NOT NULL,link TEXT NOT NULL,categorie INT)'); 

    tx.executeSql('INSERT INTO news (title,descriptionlink,categorie) VALUES (?,?,?,?)',["test1","descrip","link2",2]); 
} 


// Transaction error callback 
function errorCB(tx, err) { 
    alert("Error processing SQL: "+err); 
} 

// Transaction success callback 
// 
function successCB() { 
    alert("success!"); 
}}); 

有關此事件的詳細信息,請參閱http://docs.phonegap.com/en/2.9.0/cordova_events_events.md.html#deviceready

+0

感謝它的工作現在 –

+0

好知道.. PLZ標記答案接受時,其可能爲你。 – Purus