我正在尝试将数据插入到websql中。我在Chrome上测试它无法正常工作。我得到了error processing: SQL undefined
。
以下是代码:
$(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!");
}});
答案 0 :(得分:4)
使用PhoneGap和Cordova,您应该使用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