簡體   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