# Database persistence

**URL:** <https://forum.ionicframework.com/t/database-persistence/5853>\
**Category:** ionic-v1\
**Created:** [June 18, 2014, 11:18pm UTC](https://forum.ionicframework.com/t/database-persistence/5853 "2014-06-18T23:18:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vpease](https://avatars.discourse-cdn.com/v4/letter/v/7bcc69/32.png) [@vpease](https://forum.ionicframework.com/u/vpease)\
**Post date:** [June 18, 2014, 11:18pm UTC](https://forum.ionicframework.com/t/database-persistence/5853/1 "2014-06-18T23:18:35Z")

</div>

hi all.

i was able to use sqlite for database persistence and everything Works fine until i refresh the browser or i restart the App in the mobile. It’s like the database is 'gone once the App ends.  
what can i do to persist the database in a permanent way

to add more details, i am using the sqlite plugin for the mobile and even sample code have this behavior

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [June 18, 2014, 11:35pm UTC](https://forum.ionicframework.com/t/database-persistence/5853/2 "2014-06-18T23:35:59Z")

</div>

How are you calling your data base? In a controller of factory?

---

<div class="post-metadata">

**Author:** ![vpease](https://avatars.discourse-cdn.com/v4/letter/v/7bcc69/32.png) [@vpease](https://forum.ionicframework.com/u/vpease)\
**Post date:** [June 18, 2014, 11:41pm UTC](https://forum.ionicframework.com/t/database-persistence/5853/3 "2014-06-18T23:41:06Z")

</div>

in a factory:

```
app.factory('DB',function($q, DB_CONFIG) {
    var self = this;
    self.db = null;
    self.init = function() {
// Use self.db = window.sqlitePlugin.openDatabase({name: DB_CONFIG.name}); in production
        //self.db = window.sqlitePlugin.openDatabase(DB_CONFIG.name,"1.0",DB_CONFIG,-1);
        self.db = window.sqlitePlugin.openDatabase({name: DB_CONFIG.name});
            angular.forEach(DB_CONFIG.tables, function(table) {
                var columns = [];
                angular.forEach(table.columns, function(column) {
                    columns.push(column.name + ' ' + column.type);
                });
                var query = 'CREATE TABLE IF NOT EXISTS ' + table.name + ' (' + columns.join(',') + ')';
                self.query(query);
                console.log('Table ' + table.name + ' initialized');
            });
        };

```

i took the code from here:

> **[Factory wrapper for SQLite plugin](http://forum.ionicframework.com/t/factory-wrapper-for-sqlite-plugin/2759)**
>
> Hi, I'm trying to set up the SQLite plugin (https://github.com/brodysoft/Cordova-SQLitePlugin) for persisting my application data. I have to use the Platform object to wait until the device is ready. How would you create this service ? How can I...

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [June 19, 2014, 1:49pm UTC](https://forum.ionicframework.com/t/database-persistence/5853/4 "2014-06-19T13:49:49Z")

</div>

Hmm, DBs are not my strongest point, but you may want to check out the cordova sqlite plugin.

> <https://github.com/ionic-team/ng-cordova/blob/master/src/plugins/sqlite.js>

---

<div class="post-metadata">

**Author:** ![vpease](https://avatars.discourse-cdn.com/v4/letter/v/7bcc69/32.png) [@vpease](https://forum.ionicframework.com/u/vpease)\
**Post date:** [June 23, 2014, 4:36am UTC](https://forum.ionicframework.com/t/database-persistence/5853/5 "2014-06-23T04:36:45Z")

</div>

thank you, problem solved now!!!

---

<div class="post-metadata">

**Author:** ![fdedeh](https://avatars.discourse-cdn.com/v4/letter/f/c37758/32.png) [@fdedeh](https://forum.ionicframework.com/u/fdedeh)\
**Post date:** [July 4, 2014, 5:16am UTC](https://forum.ionicframework.com/t/database-persistence/5853/6 "2014-07-04T05:16:25Z")

</div>

@mhartington I got an error with the $cordovaSQLite:  
Error: ‘undefined’ is not a function (evaluating ‘e.transaction(function(n){n.executeSql(o,r,function(n,e){q.resolve(e)},function(n,e){q.reject(e)})})’) at ionic.bundle.js:19272

here the code in my controller:

```
    $scope.dbTest = function(){
    var db = 'testing';
    var query = '';
    $cordovaSQLite.openDB(db);
    query = 'CREATE TABLE IF NOT EXISTS MyTable (id INTEGER PRIMARY KEY ASC, text_sample TEXT, date_sample DATE)';
    $cordovaSQLite.execute(db, query, [])
    .then(function(data){
                  console.log(data);
                  });
    query = 'INSERT INTO MyTable(text_sample, date_sample) VALUES ("Registro teste","2014-05-28")';
    $cordovaSQLite.execute(db, query, [])
    .then(function(data){
                  console.log(data);
                  });
    query = 'SELECT * FROM MyTable ORDER BY id';
    $cordovaSQLite.execute(db, query, [])
    .then(function(data){
                  console.log(data);
                  });
  }

```

I’m using the last launch: 1.0.0-beta.9 Gadolinium Gator (2014-07-02)  
testing in iOS simulator and device… the DB is opened, but the $cordovaSQLite.execute(db, query, binding) function have no success… 😕
