SQLite plugin doesn't work Ionic - Apache Cordova

970 views Asked by At

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. 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:

TypeError: Cannot read property 'openDatabase' of undefined

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.

2.You are trying to test this native plugin from a web browser.

3.You have not actually installed the base SQLite plugin into your project.

On my case:

  1. $cordovaSQLite method is inside $ionicPlatform.ready function.
  2. I am testing using a a physical Android device.
  3. I have installed the plugin on my project from the following source:

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.

2

There are 2 answers

0
Milan Patel On

Your SQLite file not exist in browser you can add manual code for android. please refer this link http://gauravstomar.blogspot.in/2011/08/prepopulate-sqlite-in-phonegap.html

0
Kooldandy On

You are getting this error because you are trying to get database connection from android but using wrong function openDatabase(). This function will use in case of client (browser). Use openDb() function to get database connection from 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)");

}