Uncaught TypeError无法使用cordova 3.5调用undefined-SQLite插件的方法“opendatabase” [英] Uncaught TypeError cannot call method 'opendatabase' of undefined-SQLite plugin with cordova 3.5

查看:863
本文介绍了Uncaught TypeError无法使用cordova 3.5调用undefined-SQLite插件的方法“opendatabase”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么会发生这种情况。我在我的应用程序中使用Sqlite插件。它工作正常。我能够存储和检索数据从DB。但是问题是当我试图在主页(即应用程序起始页)检索数据时,我收到以下错误:

I don't know why does this happen. I am using Sqlite plugin in my application. It is working fine. I am able to store and retrieve data from DB. But the problem is When I am trying to retrieve data on home page(i.e, application start page) I'm getting the following error,

但是如果我在第二页使用相同的代码应用程序,它工作正常。所以我如何填充DB值应用程序起始页。

But If I use same code in second page of my application, It is working fine. So How can I populate DB values application start page.

代码,

  $( document ).ready(function() {
         redirectHomePage();
    }); 

function redirectHomePage()
{
    alert("go to home page");
     var db = window.sqlitePlugin.openDatabase({name: "test.db"});
     db.transaction(function (tx) {
     tx.executeSql("select distinct Category from Locationlog;", [], function (tx, res) {
     $("#select-choice").empty();
     var optionheading = '<option value="Select Category">Select Category</option>';
     $("#select-choice").append(optionheading);
     alert("length: "+ res.rows.length);
     for (var i = 0; i < res.rows.length; i++)
        {
           var opt  = '<option value="';
           opt += res.rows.item(i).Category;
           opt += '">';
           opt += res.rows.item(i).Category;
           opt += '</option>';
           $("#select-choice").append(opt).selectmenu('refresh');
           $("#locationList").empty();
        }

   });
  });
  $.mobile.changePage('#homePage', "slide", false, true);
}

我的应用程序主页标识是homepage。我在android平台上使用cordova-3.5。任何建议如何填充DB之前加载第一页。我试过pageshow我有同样的错误。任何其他方法?

My application home page id is "homepage". I'm working on android platform with cordova-3.5. Any suggestions how to populate DB before loading first page. I tried "pageshow" I got the same error. Any other methods?

推荐答案

如果您的项目中没有包含 sqlite Plugin ,代码中的语句就会失败。

The statement in your code will fail if you have not included the sqlite Plugin in your project.

var db = window.sqlitePlugin.openDatabase({name: "test.db"});

此外,您应该在使用cordova插件之前等待deviceready事件触发。

您可以使用 window.openDatabase()调用,该方法创建sqlite DB并且不需要sqlite插件。

下面是在应用程序中使用openDatabase调用的代码。

Also you should be waiting for the deviceready event to get fired before using cordova plugins.
You can use window.openDatabase() call which creates sqlite DB and does not need the sqlite plugin.
Below is the code for using the openDatabase call in you app.

如果您的sqlite插件正常工作,然后更改db调用。

if your sqlite plugin is working correctly then change the db call to.

var db = window.sqlitePlugin.openDatabase({name: "test.db"});

不使用sqlite插件的代码。

code that does not use the sqlite plugin.

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

    // device APIs are available
    //
    function onDeviceReady() {
        var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
        db.transaction(populateDB, errorCB, successCB);
    }

    // Populate the database
    //
    function populateDB(tx) {
        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")');
    }

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

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

上述代码段取自Cordova API文档。有关详情,请参见此处。即使文档是3.0的,它应该适用于3.5

The above code snippet is taken from the Cordova API doc. See here for details. Even though the documentation is for 3.0 it should work for 3.5

这篇关于Uncaught TypeError无法使用cordova 3.5调用undefined-SQLite插件的方法“opendatabase”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆