cordova-plugin-dbcopy/README.md

214 lines
8.7 KiB
Markdown
Raw Normal View History

2014-04-10 18:43:55 +08:00
cordova-plugin-dbcopy
=====================
2015-05-08 22:32:29 +08:00
Add a prepopulated SQLite database in your Phonegap/Cordova Android and iOS app.
2018-10-10 16:18:06 +08:00
### IMPORTANT NOTE
2017-03-16 19:37:44 +08:00
2018-10-10 16:18:06 +08:00
1. The database file may have extensions or not for e.g the db file name would be sample.db or sample.sqlite or sample. It doesn't matter what is the file extension, just remember to use the whole filename with extensions(if having one otherwise not) as a paramter when passing to the plugin methods.
2. Location Parameters in plugin function means -
2017-03-16 19:37:44 +08:00
```
location = 0; // (Disable iCloud Backup) If set will copy the database to Library/LocalDatabase. The database will not be synced by the iCloud Backup.
or
location = 1; // If set will copy the database to Library folder instead of Documents folder.
or
location = 2; // It will copy the database in the default SQLite Database directory. This is the default location for database
```
2018-10-10 16:18:06 +08:00
3. For Android, please set the target-sdk-version to minimum 26 in config.xml
2014-04-10 19:13:15 +08:00
2015-01-29 16:17:07 +08:00
2017-03-07 20:08:39 +08:00
### Database file location
2014-04-10 19:13:15 +08:00
2017-03-08 21:48:16 +08:00
The database file must be present inside the www folder.
2014-04-10 19:13:15 +08:00
2017-03-07 20:08:39 +08:00
### Installation
2015-02-10 23:28:05 +08:00
Plugin can be install with simple cordova plugin add command -
cordova plugin add https://github.com/an-rahulpandey/cordova-plugin-dbcopy.git
2015-01-21 16:21:36 +08:00
2017-01-11 18:22:30 +08:00
### Plugin Response Success and Error
2017-03-08 21:48:16 +08:00
Plugin send the response in following format in case of any error or success -
2017-01-11 18:22:30 +08:00
2017-03-07 20:08:39 +08:00
```javascript
2017-01-11 18:22:30 +08:00
{
message : "message contains the response string like Invalid DB Location or DB Doesn't Exists or Db Copied Successfully",
code: integer value such as 404, 200, 516
}
2017-03-07 20:08:39 +08:00
```
2017-03-07 20:30:57 +08:00
2017-01-11 18:22:30 +08:00
Code -
2017-03-07 20:30:57 +08:00
```
2017-03-07 20:09:52 +08:00
404 - DB or Source or Destination Doesn't exists, see message string.
516 - DB Already Exists.
200 - Called Method Executed Successfully.
2017-03-07 20:08:39 +08:00
```
### Methods
2015-01-21 16:21:36 +08:00
2017-03-08 21:48:16 +08:00
Currently there are five methods supported by the plugin.
2015-01-29 16:17:07 +08:00
2017-03-07 21:06:38 +08:00
**Copy**
2017-03-07 20:32:49 +08:00
=========================================
2017-03-07 20:30:57 +08:00
2015-07-31 18:42:40 +08:00
This Method allows you the copy the database from www directory.
2017-03-07 20:08:39 +08:00
```javascript
2015-06-12 15:33:45 +08:00
window.plugins.sqlDB.copy(dbname, location, success,error);
2017-03-07 20:08:39 +08:00
```
2015-01-29 16:26:20 +08:00
Here -
2015-01-21 16:22:36 +08:00
2017-03-07 20:08:39 +08:00
**dbname** -> Is the name of the database you want to copy. The dbname can be filename (without extensions) or filename.db or filename.sqlite. The plugin will look for and copy the file according to the filename provided here. And the same file name should be used while opening the database via [SQLitePlugin](https://github.com/litehelpers/Cordova-sqlite-storage).
2015-06-12 15:33:45 +08:00
2017-03-07 20:08:39 +08:00
**location** -> You can pass three integer arguments here (Use 0 for Android)-
2015-06-12 15:33:45 +08:00
2017-03-07 20:30:57 +08:00
```javascript
(for ios only)
2017-03-16 19:37:44 +08:00
location = 0; // (Disable iCloud Backup) If set will copy the database to Library/LocalDatabase. The database will not be synced by the iCloud Backup.
or
location = 1; // If set will copy the database to Library folder instead of Documents folder.
or
location = 2; // It will copy the database in the default SQLite Database directory. This is the default location for database
2017-03-07 20:30:57 +08:00
```
2015-06-12 15:33:45 +08:00
2017-03-07 20:08:39 +08:00
**success** -> function will be called if the db is copied sucessfully.
2015-06-12 15:33:45 +08:00
2017-03-07 20:08:39 +08:00
**error** -> function will be called if the there is some problem in copying the db or the file already exists on the location.
2017-03-08 21:41:40 +08:00
2017-03-28 15:01:14 +08:00
**Check Database Available on External Storage**
2017-03-08 21:41:40 +08:00
===============================================
```javascript
window.plugins.sqlDB.checkDbOnStorage(dbname, source, success, error);
```
Here -
**dbname** -> Name of the database file which is available on external or intenral storage
**source** -> Full native path for the folder in which db file is present. The "/" must be added at the end of path. For.eg. /sdcard/mydb/
2015-01-29 16:17:07 +08:00
2017-03-07 21:06:38 +08:00
**Copy Database from Device Storage**
2017-03-07 20:32:49 +08:00
===============================================
2017-01-11 18:22:30 +08:00
This is an untested version. Let me know if you have any suggestions. Also Pull Request are always welcome.
2017-03-07 20:08:39 +08:00
```javascript
2017-03-08 18:07:33 +08:00
window.plugins.sqlDB.copyDbFromStorage(dbname, location, source, deleteolddb, success, error);
2017-03-07 20:30:57 +08:00
```
2017-01-11 18:22:30 +08:00
Here -
2017-03-07 20:30:57 +08:00
**dbname** -> Is the name of the database you want to copy. The dbname can be filename (without extensions) or filename.db or filename.sqlite. The plugin will look for and copy the file according to the filename provided here. And the same file name should be used while opening the database via [SQLitePlugin](https://github.com/litehelpers/Cordova-sqlite-storage).
2017-01-11 18:22:30 +08:00
2017-03-07 20:30:57 +08:00
**location** -> You can pass three integer arguments here (Use 0 for Android)-
2017-01-11 18:22:30 +08:00
2017-03-07 20:30:57 +08:00
```
(for ios only)
2017-03-16 19:37:44 +08:00
location = 0; // (Disable iCloud Backup) If set will copy the database to Library/LocalDatabase. The database will not be synced by the iCloud Backup.
2017-03-07 20:30:57 +08:00
or
location = 1; // If set will copy the database to Library folder instead of Documents folder.
or
2017-03-16 19:37:44 +08:00
location = 2; // It will copy the database in the default SQLite Database directory. This is the default location for database
2017-03-07 20:30:57 +08:00
```
2017-01-11 18:22:30 +08:00
**source** -> Source File location like /sdcard/mydb/db.db. Please provide a valid existing location and the dbname should be present in the path.
2017-03-08 18:06:57 +08:00
2017-03-08 18:07:33 +08:00
**deleteolddb** -> A boolean value if set to true, will delete the existing db from the local app database folder before copying the new db. Please provide proper boolean value true or false;
2017-01-11 18:22:30 +08:00
**success** -> function will be called if the db is copied sucessfully.
**error** -> function will be called if the there is some problem in copying the db or the file already exists on the location.
2017-03-07 20:08:39 +08:00
2017-03-07 21:06:38 +08:00
**Copy Database To Device Storage**
2017-03-07 20:32:49 +08:00
============================================
2017-01-11 18:22:30 +08:00
This is an untested version. Let me know if you have any suggestions. Also Pull Request are always welcome.
2017-03-07 20:08:39 +08:00
```javascript
2017-03-14 19:37:38 +08:00
window.plugins.sqlDB.copyDbToStorage(dbname, location, destination, overwrite, success, error);
2017-03-07 20:08:39 +08:00
```
2017-01-11 18:22:30 +08:00
Here -
2017-03-07 20:30:57 +08:00
2017-01-11 18:22:30 +08:00
**dbname** -> Is the name of the database you want to copy. The dbname can be filename (without extensions) or filename.db or filename.sqlite. The plugin will look for and copy the file according to the filename provided here. And the same file name should be used while opening the database via [SQLitePlugin](https://github.com/litehelpers/Cordova-sqlite-storage).
2017-03-07 20:30:57 +08:00
2017-01-11 18:22:30 +08:00
**location** -> You can pass three integer arguments here (Use 0 for Android)-
2017-03-07 20:30:57 +08:00
```javascript
(for ios only)
2017-03-16 19:37:44 +08:00
location = 0; // (Disable iCloud Backup) If set will copy the database to Library/LocalDatabase. The database will not be synced by the iCloud Backup.
2017-03-07 20:30:57 +08:00
or
location = 1; // If set will copy the database to Library folder instead of Documents folder.
or
2017-03-16 19:37:44 +08:00
location = 2; // It will copy the database in the default SQLite Database directory. This is the default location for database
2017-03-07 20:30:57 +08:00
```
2017-03-08 18:06:57 +08:00
**destination** -> Destination File location like /sdcard/mydb/ Please provide a valid existing location and "/" should be present at the end of the path. Do not append db name in the path.
2017-03-14 19:37:38 +08:00
**overwrite** -> if set to true, then will replace the file at the destination. Otherwise will throw an error, if destination file already exists.
2017-03-07 20:30:57 +08:00
**success** -> function will be called if the db is copied sucessfully.
**error** -> function will be called if the there is some problem in copying the db or the file already exists on the location.
2017-03-07 21:06:38 +08:00
**Remove**
2017-03-07 20:32:49 +08:00
==================================
2015-01-29 16:26:20 +08:00
This method allows you to remove the database from the apps default database storage location.
2015-01-29 16:17:07 +08:00
2017-03-07 20:30:57 +08:00
```javascript
2015-06-12 23:26:16 +08:00
window.plugins.sqlDB.remove(dbname, location, success,error);
2017-03-07 20:30:57 +08:00
```
Here -
2015-01-29 16:17:07 +08:00
2017-03-07 20:30:57 +08:00
**dbname** -> Is the name of the database you want to remove. If the database file is having any extension, please provide that also.
2015-06-12 15:33:45 +08:00
2017-03-07 20:30:57 +08:00
**location** -> The integer value for the location of database, see the copy method for options.
2015-06-12 15:33:45 +08:00
2017-03-07 20:30:57 +08:00
**success** -> function will be called if the db is removed sucessfully.
2015-06-12 15:33:45 +08:00
2017-03-07 20:30:57 +08:00
**error** -> function will be called if the there is some problem in removing the db or the file doesn't exists on the default database storage location.
2015-01-21 16:21:36 +08:00
###Example Usage
2014-04-10 19:15:20 +08:00
2015-06-12 15:33:45 +08:00
In your JavaScript or HTML use the following method -
2014-04-10 19:13:15 +08:00
2017-03-07 20:08:39 +08:00
```javascript
2014-04-10 19:13:15 +08:00
function dbcopy()
{
2015-01-21 16:21:36 +08:00
//Database filename to be copied is demo.db
2015-06-12 15:33:45 +08:00
2017-03-16 19:37:44 +08:00
//location = 0, will copy the db to default SQLite Database Directory, /Library/LocalDatabase (Disable iCloud Backup)
2015-06-12 15:33:45 +08:00
window.plugins.sqlDB.copy("demo.db", 0, copysuccess,copyerror);
or
//location = 1, will copy the database to /Library folder
window.plugins.sqlDB.copy("demo.db", 1, copysuccess,copyerror);
or
2017-03-16 19:37:44 +08:00
//location = 2, will copy the database to /Documents folder
2015-06-12 15:33:45 +08:00
window.plugins.sqlDB.copy("demo.db", 2, copysuccess,copyerror);
2014-04-10 19:13:15 +08:00
}
2015-01-29 16:17:07 +08:00
function removeDB()
{
2015-06-12 15:33:45 +08:00
var location = 1;
window.plugins.sqlDB.remove("demo.db", location, rmsuccess,rmerror);
2015-01-29 16:17:07 +08:00
}
2014-04-10 19:13:15 +08:00
function copysuccess()
{
//open db and run your queries
2015-01-21 16:21:36 +08:00
db = window.sqlitePlugin.openDatabase({name: "demo.db"});.
2014-04-10 19:13:15 +08:00
}
2015-02-02 14:30:23 +08:00
function copyerror(e)
2014-04-10 19:13:15 +08:00
{
//db already exists or problem in copying the db file. Check the Log.
2015-01-29 16:17:07 +08:00
console.log("Error Code = "+JSON.stringify(e));
//e.code = 516 => if db exists
2014-04-10 19:13:15 +08:00
}
2017-03-07 20:08:39 +08:00
```