Cordova-sqlite-storage: Setting path of DB

Created on 19 Jun 2015  路  15Comments  路  Source: storesafe/cordova-sqlite-storage

Hi,
I want to set my app use DB in "storage/sdcrad0/myfolder/my.db" but when I use
var db = window.sqlitePlugin.openDatabase({name: "storage//sdcard0//myfolder//my.db", location: 1});
database cannot open and my app take error.
How can I set my app read database in my folder?
Thank a lot.

enhancement doc-todo

Most helpful comment

cordova fileplugin can copy over your files
Android default location: cordova.file.applicationStorageDirectory+"databases/your_database_name"
IOS location (with location:1 parameter) cordova.file.documentsDirectory+"your_database_name"

All 15 comments

Unfortunately this plugin does _NOT_ support opening a database from an arbitrary location. This feature is under future consideration. Also priority is given to paying customers.

Thank for your answer.
:(

When I run my app in Genymotion, I can find my data I created. But when I run my app in real device (Samsung Galaxy tab 3 - (android 4.2 and android 4.4) ), I can't find my DB in it.
Where is my DB in real device?

When I run my app in Genymotion, I can find my data I created. But when I
run my app in real device (Samsung Galaxy tab 3 - (android 4.2 and android
4.4) ), I can't find my DB in it.
Where is my DB in real device?

The sqlite database is _usually_ hidden when the app stores it in the
default location.

Thank a lot. :+1:
May I copy database into default location to use it.

You can copy to the default location in Java code. I am not sure if the Cordova File API (plugin) will be able to access the default database location.

cordova fileplugin can copy over your files
Android default location: cordova.file.applicationStorageDirectory+"databases/your_database_name"
IOS location (with location:1 parameter) cordova.file.documentsDirectory+"your_database_name"

Thank you @reconka :+1:

Adding to @reconka 's answer, you can copy your database from sdcard root folder to /data/data/your-app-id/databases using http://ngcordova.com/docs/plugins/file/ plugin

var copyDB = function() {
    $cordovaFile.checkFile(cordova.file.externalRootDirectory, "yourDB.db3")
        .then(function (success) {
            $cordovaFile.copyFile(cordova.file.externalRootDirectory, "yourDB.db3" ,
                cordova.file.applicationStorageDirectory +"/databases/")
                .then(function(success) {
                    // success
                }, function(error)
                {
                    //error
                });
        }, function (error)
        {
            //error
        });  
}

NOTE: in Kitkat, the $cordovaFile.copyFile will return error(NOT_FOUND_ERR), because the location cordova.file.applicationStorageDirectory +"/databases/" does not exist. For that, you need to first create this directory. Use the code below:

$cordovaFile.checkDir(cordova.file.applicationStorageDirectory, "databases")
        .then(function (success) {
            copyDB(); // see the function definition above
        }, function (error) {
            $cordovaFile.createDir(cordova.file.applicationStorageDirectory, "databases", false)
                .then(function (success) {
                    copyDB(); // see the function definition above
                }, function (error) {
                    // error
                });
        });

someone you ran the code??

yes, it works on android.

One can also just edit the java souce code to fit their needs. In src/android/io/sqlc/SQLitePlugin.java ca. on line 203 there's this code:

File dbfile = this.cordova.getActivity().getDatabasePath(dbname);

To access the emulated sdcard, one just needs to edit the line to:

File dbfile = new File(Environment.getExternalStorageDirectory()
                            .getAbsolutePath(), dbname);

and import the android.os.Environment in the top.

If I now want to access a database inside a folder of the emulated sdcard (/storage/emulated/0/) I just input the name myFolder/theDatabase.db.

Hope I could help somebody

The copy technique worked for me but the database is encrypted and cannot be opened. Does anybody know of a way around that? I'd like to temporarily (during development) turn off encryption so that I can open/view the database in a GUI on my Windows PC.

@niczak
You can View your database From Android studio
Tool>Android>AndroidDeviceMonitor
From AndroidDeviceMonitor you can find data>data>Yourapplication>database

It work for rooted device and also work for emulator

Other option is for temporary you can use plugin cordova-plugin-dbcopy and take it on device and you can use it easily.

@ashishradadia
works fine, Able to pullout db from android device moniter ,,,thank you

Was this page helpful?
0 / 5 - 0 ratings