Search code examples
htmlsqlitecordovaweb-applicationsphonegap

Can cordova-sqlite-ext read db on sd card (Android)?


I have searched for houres but I couldn't really figure out wheter I can read a sqlite file on my sd card (phone's device storage) on android using the cordova-sqlite-ext plugin.

On https://www.npmjs.com/package/cordova-sqlite-storage it says that:

The following features are available in litehelpers / cordova-sqlite-ext: ... - Pre-populated database (Android/iOS/macOS/Windows)

So what I need is a sqlite database outside the webapp and a phonegap plugin that can read from this db. So, is it correct that the plugin above can do that???

Or is there any other way how to accomplish that task?


Solution

  • cordova-sqlite-storage stores its database in the private storage directory of the app. This is accessible only to your app and is located on the internal data partition. For example, if your app package ID is foo.bar.com and your database has name: store.db then it will be located at /data/data/foo.bar.com/databases/store.db. The location data/data/foo.bar.com/ is referenced as cordova.file.applicationStorageDirectory from cordova-plugin-file.

    You can use the cordova-sqlite-evcore-extbuild-free variant of cordova-sqlite-storage:

    Custom Android database location (supports external storage directory)

    The "external storage directory" is on the "SD card" which is usually the internal memory partition accessed via the mount points /sdcard/ or /storate/emulated/0/. Since Android 4.4, apps only have write access in the "application sandbox" directory on the SD card e.g. /sdcard/Android/data/foo.bar.com/ (cordova.file.externalApplicationStorageDirectory).

    All other areas of the SD card are read-only (e.g. the root of /sdcard/ - cordova.file.externalRootDirectory) so while you could read from a database here, to write to it you'd need to copy it to either the private or external storage directory of the app. You could do this using cordova-plugin-file, for example.