Open a sqlite database


#1

I have installed the plugin which enable me to read an sqlite database but it doesn’t work .

here is the code :

var db = null;

function dbcopy()
{
        //Database filename to be copied is demo.db
        window.plugins.sqlDB.copy("digicode.sqlite",copysuccess,copyerror);
}

function copysuccess()
{
        //open db and run your queries
         db = window.sqlitePlugin.openDatabase({name: "digicode.sqlite"});
         alert(db);
}

function copyerror(e)
{
        //db already exists or problem in copying the db file. Check the Log.
        console.log("Error Code = "+JSON.stringify(e));
        //e.code = 516 => if db exists
}

var app = angular.module('starter', ['ionic','ngCordova'])

.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);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
    if (window.sqlitePlugin) {
      dbcopy();
    }
  });
})



app.controller("digicodeController", ["$cordovaSQLite",function($cordovaSQLite){
 
  this.digicode = {};

  this.recupererCode = function(digi){
    //alert(digi.date+" "+digi.salle);
    //  Test
    var query = "SELECT day FROM salles_digicode";
        $cordovaSQLite.execute(db, query, []).then(function(res) {
            if(res.rows.length > 0) {
                alert("SELECTED -> " + res.rows.item(0).day);
            } else {
                alert("No results found");
            }
        }, function (err) {
            console.error(err);
        });
    }

}]);

#2

I see you are using Web SQLite, can you tell me on what device are you testing this? Everyone on internet claims Web SQLite is working on all android devices but i found out that’s not the case.
But caniuse mentions that samsung android devices are not supporting it, and i found out that it doesn’t work on a lot more devices.
You should check out native SQLite plugin for PhoneGap that keeps DB on device. This one: https://github.com/brodysoft/Cordova-SQLitePlugin


#3

This might help you:

Without knowing what errors you are getting, it will be impossible for us to help you troubleshoot. Please share your logs, what you’re testing with (browser, device, simulator), and what platform.

The more you share, the better we can help :smile:

Regards,


#4

I’m using an emulator :

id: 1 or "android-19"
Name: Android 4.4.2
Type: Platform
API level: 19
Revision: 4
Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800, WXGA800-7in
Tag/ABIs : default/armeabi-v7a, default/x86

but i put the file in the www folder i don’t know i it is here that i have to put it.


#5

When i test on the browser i got that :

TypeError: Cannot read property 'transaction' of null
    at Object.execute (file:///home/emmanuel/Android/ExampleProject/www/js/ng-cordova.min.js:9:16224)
    at recupererCode (file:///home/emmanuel/Android/ExampleProject/www/js/app.js:59:24)
    at $parseFunctionCall (file:///home/emmanuel/Android/ExampleProject/www/lib/ionic/js/ionic.bundle.js:20124:18)
    at callback (file:///home/emmanuel/Android/ExampleProject/www/lib/ionic/js/ionic.bundle.js:30728:17)
    at Scope.$eval (file:///home/emmanuel/Android/ExampleProject/www/lib/ionic/js/ionic.bundle.js:22178:28)
    at Scope.$apply (file:///home/emmanuel/Android/ExampleProject/www/lib/ionic/js/ionic.bundle.js:22276:23)
    at HTMLFormElement.<anonymous> (file:///home/emmanuel/Android/ExampleProject/www/lib/ionic/js/ionic.bundle.js:30733:23)
    at HTMLFormElement.eventHandler (file:///home/emmanuel/Android/ExampleProject/www/lib/ionic/js/ionic.bundle.js:10823:21)
    at triggerMouseEvent (file:///home/emmanuel/Android/ExampleProject/www/lib/ionic/js/ionic.bundle.js:2811:7)
    at tapClick (file:///home/emmanuel/Android/ExampleProject/www/lib/ionic/js/ionic.bundle.js:2800:3)

But to debug on the device sorry i still don’t know how to do, i will look for …


#6

Native device plugins such as SQLite do not work in the browser. You need to test on a device or simulator.

Regards,


#7

Yes, i tested with a simulator but it still doesn’t work. When i create an sqlite database, where do i have to put the database in the www folder ?


#8

The log you pasted says you did not test in a simulator or device. It says you tested from your web browser. You need to compile an APK and install it to the device or simulator.

If you’ve run another test and have new logs, please paste them.

Regards,


#9

Sorry for the time … To debug i run this command ionic run android -l -c i don’t know if it is like that we debug but i got that on my console at the beginning :

0     470125   error    Uncaught TypeError: Cannot read property 'sqlDB' of undefined, http://192.168.0.10:8100/js/app.js, Line: 14 

when i clicked on my button i got that :

1     562525   error    TypeError: Cannot call method 'transaction' of null
    at Object.execute (http://192.168.0.10:8100/js/ng-cordova.min.js:9:16225)
    at recupererCode (http://192.168.0.10:8100/js/app.js:59:24)
    at $parseFunctionCall (http://192.168.0.10:8100/lib/ionic/js/ionic.bundle.js:20124:18)
    at callback (http://192.168.0.10:8100/lib/ionic/js/ionic.bundle.js:30728:17)
    at Scope.$eval (http://192.168.0.10:8100/lib/ionic/js/ionic.bundle.js:22178:28)
    at Scope.$apply (http://192.168.0.10:8100/lib/ionic/js/ionic.bundle.js:22276:23)
    at HTMLFormElement.<anonymous> (http://192.168.0.10:8100/lib/ionic/js/ionic.bundle.js:30733:23)
    at HTMLFormElement.eventHandler (http://192.168.0.10:8100/lib/ionic/js/ionic.bundle.js:10823:21)
    at triggerMouseEvent (http://192.168.0.10:8100/lib/ionic/js/ionic.bundle.js:2811:7)
    at tapClick (http://192.168.0.10:8100/lib/ionic/js/ionic.bundle.js:2800:3)

#10

You are mixing things. the plugin is on:
$window.sqlitePlugin

in your dbcopy method your are using window.plugins.sqlDB, which dies not exists i think.
In other parts you are using the cordocaSQLite syntax…

Geets, bengtler


#11

Hi.
I am getting the same error…Cannot call method ‘transaction’. I used the phonegap desktop application to start the server and tested it in device via a shared network.
I tried $cordovaSQLite.openDB(‘mydb.db’) syntax. I learnt this from the ngCordova.js file.
Can some one explain me why this line returns a null value?
@nicraboy, I used your tutorial. Can you help me out?
Thanks!


#12

Hi ,
Did you try to open and create database in run function like this link https://blog.nraboy.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/


#13

See the best example: https://github.com/jdnichollsc/Ionic-Starter-Template

Regards, Nicholls