PhoneGap SQLite错误?未捕获的TypeError:对象#<object>没有方法&#39; exec&#39; </object>

时间:2013-02-12 11:50:39

标签: android sqlite cordova

我正在开发Android应用程序。我正在将sqlite集成到我的应用程序https://github.com/brodyspark/PhoneGap-sqlitePlugin-Android中 以下错误即将来临

  

未捕获的TypeError:对象#没有方法'exec'

使用以下代码

window.sqlitePlugin.openDatabase({name: "DB"});

3 个答案:

答案 0 :(得分:1)

您需要确保在打开数据库之前等待Cordova加载。

根据README.md from the project

// Wait for Cordova to load
document.addEventListener("deviceready", onDeviceReady, false);

// Cordova is ready
function onDeviceReady() {
  var db = window.sqlitePlugin.openDatabase({name: "my.db"});
  // ...
}

答案 1 :(得分:1)

https://github.com/xuexueMaGicK/Gift-App

看到这个链接js文件可以在这里找到数据库连接

 window.addEventListener("DOMContentLoaded", init);

function init() {
pageshow = document.createEvent("Event");
pageshow.initEvent("pageshow", true, true);

tap = document.createEvent("Event");
tap.initEvent("tap", true, true);

pages = document.querySelectorAll('[data-role="page"]');
numPages = pages.length;

links = document.querySelectorAll('[data-role="link"]');
numLinks = links.length;

//checkDB();
document.addEventListener("deviceready", checkDB, false);
 }

/ <强> *********        一般互动 的 ********* /

 function checkDB() {
 navigator.splashscreen.hide();

 database = openDatabase('data', '', 'data', 1024 * 1024);

 if (database.version === '') {

    database.changeVersion('', '1.0', createDB, function (tx, err) {
        console.log(err.message);
    }, function (tx, rs) {
        console.log("Increment transaction success.");
    });
    addNavHandlers();
} else {
    addNavHandlers();
}

}

function createDB(db) 
{

/*******Create Table Gifts********/

 db.executeSql('CREATE TABLE "gifts" ("gift_id" INTEGER PRIMARY KEY AUTOINCREMENT, "name_id" INTEGER, "occasion_id" INTEGER, "gift_idea" VARCHAR(45))', [], function (tx, rs) {
    console.log("Table gifts created");
}, function (tx, err) {
    console.log(err.message);
});
/*******Create Table Names********/
db.executeSql('CREATE TABLE "names" ("name_id" INTEGER PRIMARY KEY AUTOINCREMENT, "name_text" VARCHAR(80))', [], function (tx, rs) {
    console.log("Table names created");
}, function (tx, err) {
    console.log(err.message);
});
/*******Create Table Occasions********/
db.executeSql('CREATE TABLE "occasions" ("occasion_id" INTEGER PRIMARY KEY AUTOINCREMENT, "occasion_text" VARCHAR(80))', [], function (tx, rs) {
    console.log("Table occasions created");
}, function (tx, err) {
    console.log(err.message);
});

}

答案 2 :(得分:0)

在Manifest.xml中你必须添加与SQLite相关的插件。然后它才能工作。