简体   繁体   English

SQLite插件不适用于Ionic-Apache Cordova

[英]SQLite plugin doesn't work Ionic - Apache Cordova

I am developing an Apache Cordova app for Android and IOS using Ionic framework too, for this I need to have a little database in local storage and I wan't to use SQLite. 我也正在使用Ionic框架开发适用于Android和IOS的Apache Cordova应用程序,为此,我需要在本地存储中拥有一个小数据库,而我不想使用SQLite。 I follow a lot of tutorials and in fact this is very easy -at least it seems-, but I have the follow error: 我遵循了很多教程,实际上,这很容易-至少看起来很简单-但我有以下错误:

Uncaught TypeError: Cannot read property 'openDatabase' of undefined        (11:52:54:666 | error, javascript)
at (anonymous function) (www/js/app.js:47:29)
at (anonymous function) (www/lib/ionic/js/ionic.bundle.js:53329:19)
at onPlatformReady (www/lib/ionic/js/ionic.bundle.js:2491:24)
at onWindowLoad (www/lib/ionic/js/ionic.bundle.js:2472:7)

After this error, the database is not created, in other words, it doesn't works. 发生此错误后,将不会创建数据库,也就是说,它将无法正常工作。 Obviously I resarch about this in the internet and I found a lot of possible solutions, the most aproximated solution that can explain this and I found is this: 显然,我在Internet上对此进行了研究,并且找到了许多可能的解决方案,可以解释这一问题的最接近的解决方案是:

TypeError: Cannot read property 'openDatabase' of undefined TypeError:无法读取未定义的属性“ openDatabase”

But for my doesn't Works :( 但是对我来说不起作用:(

To quote from the last link: 引用最后一个链接:

It is happening for one of a few reasons: 发生这种情况的原因如下:

1.You are not wrapping the $cordovaSQLite methods in the $ionicPlatform.ready() function. 1.您没有将$ cordovaSQLite方法包装在$ ionicPlatform.ready()函数中。

2.You are trying to test this native plugin from a web browser. 2.您正在尝试通过网络浏览器测试此本机插件。

3.You have not actually installed the base SQLite plugin into your project. 3.您尚未将基本的SQLite插件实际安装到项目中。

On my case: 就我而言:

  1. $cordovaSQLite method is inside $ionicPlatform.ready function. $ cordovaSQLite方法位于$ ionicPlatform.ready函数中。
  2. I am testing using aa physical Android device. 我正在使用物理Android设备进行测试。
  3. I have installed the plugin on my project from the following source: 我已经从以下来源在我的项目上安装了插件:

https://github.com/brodysoft/Cordova-SQLitePlugin-2014.07.git https://github.com/brodysoft/Cordova-SQLitePlugin-2014.07.git

As I said I research a lot about this and I don't achive solve this situation. 正如我所说的,我对此进行了很多研究,但我没有解决这个问题。

My code: 我的代码:

params.run(function($ionicPlatform, $cordovaSQLite) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins.Keyboard) {
  cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
  cordova.plugins.Keyboard.disableScroll(true);

}
if (window.StatusBar) {
  // org.apache.cordova.statusbar required
  StatusBar.styleDefault();
}

db = window.sqlitePlugin.openDatabase({name: "inspeccionesDB.db"});   
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS table1 (id integer primary key autoincrement not null, company char(255) not null, cif char(20) not null, state char(20) not null, related_ids char(45) not null)");
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS table2 (id integer primary key not null, code char(20) not null, firstname char(20) not null, surname char(100) not null, surname1 char(100) not null, nif char(20) not null, expired_passport_dt text not null, is_valid tinyint not null");
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS table3 (id integer primary key not null, code char(20), address char(250), location char(100), provincia(250))");    
});
});

I will be absolutely thankful for any advice or possible solution. 对于任何建议或可能的解决方案,我将深表感谢。

Thanks in advance to everybody. 在此先感谢大家。

You are getting this error because you are trying to get database connection from android but using wrong function openDatabase(). 您收到此错误的原因是您尝试从android获取数据库连接,但使用了错误的函数openDatabase()。 This function will use in case of client (browser). 此功能将在客户端(浏览器)的情况下使用。 Use openDb() function to get database connection from android. 使用openDb()函数从android获取数据库连接。

    if (window.cordova) {
// device
       dbconn = $cordovaSQLite.openDB({ name: "my.db" , location: 'default'}); 
       $cordovaSQLite.execute(dbconn, "CREATE TABLE IF NOT EXISTS people (id integer primary key, firstname text, lastname text)");
    }
    else{
// browser
    dbconn = window.openDatabase("my.db", '1', 'my', 1024 * 1024 * 100);
    $cordovaSQLite.execute(dbconn, "CREATE TABLE IF NOT EXISTS people (id integer primary key, firstname text, lastname text)");

}

Your SQLite file not exist in browser you can add manual code for android. 您的SQLite文件在浏览器中不存在,您可以为Android添加手动代码。 please refer this link http://gauravstomar.blogspot.in/2011/08/prepopulate-sqlite-in-phonegap.html 请参考此链接http://gauravstomar.blogspot.in/2011/08/prepopulate-sqlite-in-phonegap.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM