Cordova-sqlite-storage: DB file location [Where to save db file]

Created on 24 Mar 2015  路  13Comments  路  Source: storesafe/cordova-sqlite-storage

Great plugin,what i don't understand is where to put the database file and be able to read it in my app for both testing and normal usage on devices.

Thanks.

question doc-general

Most helpful comment

Ok, let me copy/paste snippet of my code, I hope that it would be clear:
config.js

    this.dbName = 'location.db';
    this.packageName = 'com.example.app1';
    this.dbLocalFileUrl = {android: '/data/data/' + this.packageName + '/databases/' +
                                    this.dbName,
                           ios: 'cdvfile://localhost/persistent/' + this.dbName,
                           win32nt: '/' + this.dbName,
    };                          

util.js

// Fetches an sqlite DB from an url
Util.prototype.populate = function(onFileDownloaded, url, fileUrl) {
    // Requires https://github.com/apache/cordova-plugin-file-transfer/blob/master/doc/index.md
    var fileTransfer = new FileTransfer();
    var uri = encodeURI(url);

    fileTransfer.download(
        uri,
        //this.getFileUrl(),
        fileUrl,
                onFileDownloaded,
        function(error) {
            console.log("download error source " + error.source);
            console.log("download error target " + error.target);
            console.log("upload error code" + error.code);
        },
        false,
        {
            headers: {
                "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
            }
        }
    );
}

controller.js

        // Async
        util.populate(function fileDownloaded(entry) {
            console.log("download complete: " + entry.toURL());
        }, config.dbUrl, location.getFileUrl());
        // End Async

Cheers.

All 13 comments

Thanks! I am not sure if I really understand your question. When the app calls sqlitePlugin.openDatabase(), the plugin will create the database file if it does not exist, or read the database file if it does exist. For each platform, the database file should be in the same path (relative to your app) regardless whether you are running it on an emulator or on a real device.

There is a createFromLocation option that copies the database file from the app's www directory if it is not already present (pre-populated database), documented in README.md.

Please let me know if this answers your question or not.

Hi there.

Just in case that the App needs to download the sqlite DB from internet and save it in the device below the summary of the locations (quick and dirty, but I guess its readable):

this.dbLocalFileUrl = {android: '/data/data/' + this.packageName + '/databases/' + this.dbName,
ios: 'cdvfile://localhost/persistent/' + this.dbName,
win32nt: '/' + this.dbName,
};
Cheers.

Hi,
I will try to clarify my question,i did create a database using an sqlite tool,i have the database file ready to use,i just need to know where to save it in my cordova app project,and should i use createFromLocation in this case ?
Thanks.

Ok, let me copy/paste snippet of my code, I hope that it would be clear:
config.js

    this.dbName = 'location.db';
    this.packageName = 'com.example.app1';
    this.dbLocalFileUrl = {android: '/data/data/' + this.packageName + '/databases/' +
                                    this.dbName,
                           ios: 'cdvfile://localhost/persistent/' + this.dbName,
                           win32nt: '/' + this.dbName,
    };                          

util.js

// Fetches an sqlite DB from an url
Util.prototype.populate = function(onFileDownloaded, url, fileUrl) {
    // Requires https://github.com/apache/cordova-plugin-file-transfer/blob/master/doc/index.md
    var fileTransfer = new FileTransfer();
    var uri = encodeURI(url);

    fileTransfer.download(
        uri,
        //this.getFileUrl(),
        fileUrl,
                onFileDownloaded,
        function(error) {
            console.log("download error source " + error.source);
            console.log("download error target " + error.target);
            console.log("upload error code" + error.code);
        },
        false,
        {
            headers: {
                "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
            }
        }
    );
}

controller.js

        // Async
        util.populate(function fileDownloaded(entry) {
            console.log("download complete: " + entry.toURL());
        }, config.dbUrl, location.getFileUrl());
        // End Async

Cheers.

On Tue, Mar 24, 2015 at 10:47 AM, MohamedMehrez [email protected] wrote:

Hi,
I will try to clarify my question,i did create a database using an sqlite tool,i have the database file ready to use,i just need to know where to save it in my cordova app project,and should i use createFromLocation in this case ?
Thanks.

@MohamedMehrez: the createFromLocation option is the supported way to use the pre-populated database that you create using the sqlite tool, as described in the readme. If you don't see your data from the sqlite tool, please remove your app from the emulator or device and try it again.

The only exception is for WP(8), since the createFromLocation option is not supported for the WP(8) platform. For WP(8), please use the File API plugin to install your pre-populated database.

Thanks @jlegido for your input! The DB location will be documented (in the readme).

Some notes:

  • The title of this question was changed, since it can help others find where the database file is stored.
  • The sample by @jlegido applies to the case when the (pre-populated) database file is downloaded from a URL. It should be very easy to adapt it to use the File API instead.
  • The WP(8) version will eventually be superseded by the upcoming Windows version (see #129), which will support Universal Windows apps for Windows 8.0, Windows 8.1(+), and Windows Phone 8.1(+). The Windows version will be much more efficient and robust due to direct binding between Javascript and C++. The WP(8) version, which _should_ also work on Windows Phone 7.0 (I was not able to test it), was written in C# by @marcucio (Mike Arcucio) and has served as an excellent bridge into the Windows (Phone) world. Unfortunately the WP(8) version uses the CSharp-SQLite library which was reported to be very buggy (workaround needed for DROP TABLE; memory problems after 1000 transactions or so).

Thank you so much guys,i'm getting used to it now,the only problem is that i get a bunch of errors and i'm trying to fix it,have anyone experienced any problems when using angularjs with this plugin ?

Some people have raised questions in the past about using the plugin with Ionic (which is built on AngularJS). There is an excellent description how to use the plugin with Ionic here: https://blog.nraboy.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/

and a gist to use with AngularJS but without Ionic here: https://gist.github.com/jgoux/10738978

This needs to be documented in the README!

Just to add a comment , I have experienced the problem with the DB not copying a pre-polulated one but dropping to a certain version of the plugin fixed the issues ( as below );

cordova plugin add [email protected]

Its an awesome plugin though @brodybits #legend.

As stated in README.md, pre-populated database is now supported in litehelpers / cordova-sqlite-ext. Continuing this discussion in litehelpers/cordova-sqlite-ext/issues/7.

I am currently using the sqlite helper an ionic app and i get this error in my adb.

I/chromium( 1622): [INFO:CONSOLE(26794)] "Error: Database location or iosDatabaseLocation value is now mandatory in openDatabase call
I/chromium( 1622): at newSQLError (file:///android_asset/www/plugins/cordova-sqlite-storage/www/SQLitePlugin.js:26:18)
I/chromium( 1622): at Object. (file:///android_asset/www/plugins/cordova-sqlite-storage/www/SQLitePlugin.js:565:15)
I/chromium( 1622): at Object.openDatabase (file:///android_asset/www/plugins/cordova-sqlite-storage/www/SQLitePlugin.js:59:20)
I/chromium( 1622): at Object.openDB (file:///android_asset/www/lib/ngCordova/dist/ng-cordova.min.js:9:23623)
I/chromium( 1622): at Scope.$scope.execute (file:///android_asset/www/js/home.js:34:28)

Hi there,

I am working on hybrid development for one of my sap fiori project using cordova.
This application will have offline functionality also.

For storing data (data fron oData services), I am using cordova-sqlite-storage plugin and able to create database, tables and store data into it. Now, when I console result for stored data, it is not showing any format so not able to fetch it for my application. Would be great help if any of you can suggest me solution for this.

Thanks!
Aarti

I am currently using the sqlite helper an ionic app and i get this error in my adb.

I/chromium( 1622): [INFO:CONSOLE(26794)] "Error: Database location or iosDatabaseLocation value is now mandatory in openDatabase call

Please read the error message and the documentation very carefully. All of the information is there.

I suggest you try the following:

Now, when I console result for stored data, it is not showing any format so not able to fetch it for my application. Would be great help if any of you can suggest me solution for this.

There is not enough information to understand why it goes wrong for you. I suggest you try the following:

Was this page helpful?
0 / 5 - 0 ratings