繁体   English   中英

SQLite插件不适用于Ionic-Apache Cordova

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

我也正在使用Ionic框架开发适用于Android和IOS的Apache Cordova应用程序,为此,我需要在本地存储中拥有一个小数据库,而我不想使用SQLite。 我遵循了很多教程,实际上,这很容易-至少看起来很简单-但我有以下错误:

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)

发生此错误后,将不会创建数据库,也就是说,它将无法正常工作。 显然,我在Internet上对此进行了研究,并且找到了许多可能的解决方案,可以解释这一问题的最接近的解决方案是:

TypeError:无法读取未定义的属性“ openDatabase”

但是对我来说不起作用:(

引用最后一个链接:

发生这种情况的原因如下:

1.您没有将$ cordovaSQLite方法包装在$ ionicPlatform.ready()函数中。

2.您正在尝试通过网络浏览器测试此本机插件。

3.您尚未将基本的SQLite插件实际安装到项目中。

就我而言:

  1. $ cordovaSQLite方法位于$ ionicPlatform.ready函数中。
  2. 我正在使用物理Android设备进行测试。
  3. 我已经从以下来源在我的项目上安装了插件:

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

正如我所说的,我对此进行了很多研究,但我没有解决这个问题。

我的代码:

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))");    
});
});

对于任何建议或可能的解决方案,我将深表感谢。

在此先感谢大家。

您收到此错误的原因是您尝试从android获取数据库连接,但使用了错误的函数openDatabase()。 此功能将在客户端(浏览器)的情况下使用。 使用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)");

}

您的SQLite文件在浏览器中不存在,您可以为Android添加手动代码。 请参考此链接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