0

我也在使用 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)

在这个错误之后,数据库没有被创建,换句话说,它不工作。显然,我在互联网上对此进行了研究,发现了很多可能的解决方案,可以解释这一点的最近似的解决方案是:

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

但对于我的不起作用:(

引用最后一个链接:

它的发生有以下几个原因之一:

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

2.您正在尝试从 Web 浏览器测试此本机插件。

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

我将非常感谢任何建议或可能的解决方案。

提前感谢大家。

4

2 回答 2

1

您收到此错误是因为您尝试从 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)");

}
于 2016-05-29T17:49:47.293 回答
0

您的 SQLite 文件在浏览器中不存在,您可以为 android 添加手动代码。请参考此链接http://gauravstomar.blogspot.in/2011/08/prepopulate-sqlite-in-phonegap.html

于 2016-04-08T11:57:07.007 回答