# Open a sqlite database

**URL:** <https://forum.ionicframework.com/t/open-a-sqlite-database/17014>\
**Category:** ionic-v1\
**Created:** [February 5, 2015, 9:43am UTC](https://forum.ionicframework.com/t/open-a-sqlite-database/17014 "2015-02-05T09:43:34Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![nooma](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@nooma](https://forum.ionicframework.com/u/nooma)\
**Post date:** [February 5, 2015, 9:43am UTC](https://forum.ionicframework.com/t/open-a-sqlite-database/17014/1 "2015-02-05T09:43:34Z")

</div>

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

}]);
```

---

<div class="post-metadata">

**Author:** ![mladen5](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mladen5/32/68585_2.png) [@mladen5](https://forum.ionicframework.com/u/mladen5)\
**Post date:** [February 5, 2015, 3:43pm UTC](https://forum.ionicframework.com/t/open-a-sqlite-database/17014/2 "2015-02-05T15:43:46Z")

</div>

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](https://github.com/brodysoft/Cordova-SQLitePlugin)

---

<div class="post-metadata">

**Author:** ![nicraboy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/nicraboy/32/6917_2.png) [@nicraboy](https://forum.ionicframework.com/u/nicraboy)\
**Post date:** [February 5, 2015, 4:02pm UTC](https://forum.ionicframework.com/t/open-a-sqlite-database/17014/3 "2015-02-05T16:02:35Z")

</div>

This might help you:

> **[Deploy Ionic Framework App With Pre-Filled SQLite DB](https://www.thepolyglotdeveloper.com/2015/01/deploy-ionic-framework-app-pre-filled-sqlite-db/)**
>
> Deploy your Ionic Framework Android and iOS mobile application with a pre-populated SQLite database using various Apache Cordova plugins and ngCordova.

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 😄

Regards,

---

<div class="post-metadata">

**Author:** ![nooma](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@nooma](https://forum.ionicframework.com/u/nooma)\
**Post date:** [February 5, 2015, 5:25pm UTC](https://forum.ionicframework.com/t/open-a-sqlite-database/17014/4 "2015-02-05T17:25:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nooma](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@nooma](https://forum.ionicframework.com/u/nooma)\
**Post date:** [February 5, 2015, 5:36pm UTC](https://forum.ionicframework.com/t/open-a-sqlite-database/17014/5 "2015-02-05T17:36:13Z")

</div>

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 …

---

<div class="post-metadata">

**Author:** ![nicraboy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/nicraboy/32/6917_2.png) [@nicraboy](https://forum.ionicframework.com/u/nicraboy)\
**Post date:** [February 5, 2015, 11:57pm UTC](https://forum.ionicframework.com/t/open-a-sqlite-database/17014/6 "2015-02-05T23:57:35Z")

</div>

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

Regards,

---

<div class="post-metadata">

**Author:** ![nooma](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@nooma](https://forum.ionicframework.com/u/nooma)\
**Post date:** [February 7, 2015, 9:55am UTC](https://forum.ionicframework.com/t/open-a-sqlite-database/17014/7 "2015-02-07T09:55:47Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![nicraboy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/nicraboy/32/6917_2.png) [@nicraboy](https://forum.ionicframework.com/u/nicraboy)\
**Post date:** [February 7, 2015, 4:12pm UTC](https://forum.ionicframework.com/t/open-a-sqlite-database/17014/8 "2015-02-07T16:12:16Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![nooma](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@nooma](https://forum.ionicframework.com/u/nooma)\
**Post date:** [February 8, 2015, 2:41pm UTC](https://forum.ionicframework.com/t/open-a-sqlite-database/17014/9 "2015-02-08T14:41:44Z")

</div>

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)
```

---

<div class="post-metadata">

**Author:** ![bengtler](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bengtler/32/98565_2.png) [@bengtler](https://forum.ionicframework.com/u/bengtler)\
**Post date:** [February 9, 2015, 7:00am UTC](https://forum.ionicframework.com/t/open-a-sqlite-database/17014/10 "2015-02-09T07:00:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Venkateshwar](https://avatars.discourse-cdn.com/v4/letter/v/c37758/32.png) [@Venkateshwar](https://forum.ionicframework.com/u/Venkateshwar)\
**Post date:** [July 7, 2015, 3:58pm UTC](https://forum.ionicframework.com/t/open-a-sqlite-database/17014/11 "2015-07-07T15:58:30Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![obcure](https://avatars.discourse-cdn.com/v4/letter/o/57b2e6/32.png) [@obcure](https://forum.ionicframework.com/u/obcure)\
**Post date:** [August 28, 2015, 8:39pm UTC](https://forum.ionicframework.com/t/open-a-sqlite-database/17014/12 "2015-08-28T20:39:32Z")

</div>

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/](https://blog.nraboy.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/)

---

<div class="post-metadata">

**Author:** ![jdnichollsc](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/jdnichollsc/32/137196_2.png) [@jdnichollsc](https://forum.ionicframework.com/u/jdnichollsc)\
**Post date:** [April 7, 2016, 10:19pm UTC](https://forum.ionicframework.com/t/open-a-sqlite-database/17014/13 "2016-04-07T22:19:33Z")

</div>

See the best example: [https://github.com/jdnichollsc/Ionic-Starter-Template](https://github.com/jdnichollsc/Ionic-Starter-Template)

Regards, Nicholls
