From c7507bbbfbc844fb309dace531f811381f32ffb5 Mon Sep 17 00:00:00 2001 From: Rahul Pandey Date: Tue, 17 Jul 2018 13:02:41 +0530 Subject: [PATCH] Fix DB Copy Issue #52 --- package.json | 14 ++++---- plugin.xml | 15 ++++----- src/android/DatabaseHelper.java | 28 ++++++++++++++-- src/android/sqlDB.java | 59 +++++++++++++++++++++++++-------- 4 files changed, 85 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 9177aba..c3c178e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cordova-plugin-dbcopy", - "version": "2.0.0", + "version": "2.1.0", "description": "Cordova/Phonegap plugin to copy SQLite Database from www directory to app database directory", "cordova": { "id": "cordova-plugin-dbcopy", @@ -23,16 +23,14 @@ "database copy", "sqlite database copy" ], - "engines": [ - { - "name": "cordova", - "version": ">=3.0.0" - } - ], + "engines": [{ + "name": "cordova", + "version": ">=3.0.0" + }], "author": "Rahul Pandey", "license": "Apache 2.0", "bugs": { "url": "https://github.com/an-rahulpandey/cordova-plugin-dbcopy/issues" }, "homepage": "https://github.com/an-rahulpandey/cordova-plugin-dbcopy" -} +} \ No newline at end of file diff --git a/plugin.xml b/plugin.xml index 6b7ce36..1600fcf 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,20 +1,19 @@ - + sqlDB Copy SQLite Database to Document Directory - Apache 2.0 + Apache 2.0 - Rahul Pandey + Rahul Pandey - + - + diff --git a/src/android/DatabaseHelper.java b/src/android/DatabaseHelper.java index cb12c97..7f0d2ea 100644 --- a/src/android/DatabaseHelper.java +++ b/src/android/DatabaseHelper.java @@ -8,6 +8,7 @@ import java.io.InputStream; import java.io.OutputStream; import android.content.Context; +import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; @@ -21,6 +22,8 @@ import org.json.JSONObject; public class DatabaseHelper extends SQLiteOpenHelper { private Context myContext; + private SQLiteDatabase database; + private String databasePath; public DatabaseHelper(Context context) { super(context, sqlDB.dbname, null, 1); @@ -30,7 +33,9 @@ public class DatabaseHelper extends SQLiteOpenHelper { public void createdatabase(File dbPath, String source, final CallbackContext callbackContext) throws IOException { - // Log.d("CordovaLog","Inside CreateDatabase = "+dbPath); + Log.d("CordovaLog","Inside CreateDatabase getAbsolutePath= "+dbPath.getAbsolutePath()); + Log.d("CordovaLog","Inside CreateDatabase path = "+dbPath.getPath()); + databasePath = dbPath.getAbsolutePath(); this.getReadableDatabase(); try { copyDatabase(dbPath, source, callbackContext); @@ -63,11 +68,13 @@ public class DatabaseHelper extends SQLiteOpenHelper { myOutput.close(); myInput.close(); try { + this.openDataBase(); response.put("message", "Db Copied"); response.put("code", 200); plresult = new PluginResult(PluginResult.Status.OK,response); + this.close(); callbackContext.sendPluginResult(plresult); - } catch (JSONException err) { + } catch (Exception err) { throw new Error(err.getMessage()); } } catch (Exception e) { @@ -83,6 +90,23 @@ public class DatabaseHelper extends SQLiteOpenHelper { } } + public void openDataBase() throws SQLException { + + //Open the database + database = SQLiteDatabase.openDatabase(databasePath, null, SQLiteDatabase.OPEN_READONLY); + + } + + @Override + public synchronized void close() { + + if(database != null) + database.close(); + + super.close(); + + } + @Override public void onCreate(SQLiteDatabase db) { diff --git a/src/android/sqlDB.java b/src/android/sqlDB.java index 15c4c74..d4f3101 100644 --- a/src/android/sqlDB.java +++ b/src/android/sqlDB.java @@ -1,5 +1,7 @@ package me.rahul.plugins.sqlDB; +import android.util.Log; + import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaPlugin; import org.apache.cordova.PluginResult; @@ -97,7 +99,7 @@ public class sqlDB extends CordovaPlugin { .getActivity().getApplicationContext()); dbpath = this.cordova.getActivity().getDatabasePath(dbname); Boolean dbexists = dbpath.exists(); - //Log.d("CordovaLog", "DatabasePath = " + dbpath + "&&&& dbname = " + dbname + "&&&&DB Exists =" + dbexists); + Log.d("CordovaLog", "DatabasePath = " + dbpath + "&&&& dbname = " + dbname + "&&&&DB Exists =" + dbexists); if (dbexists) { sendPluginResponse(516, "DB Already Exists", true, callbackContext); @@ -106,19 +108,50 @@ public class sqlDB extends CordovaPlugin { @Override public void run() { - PluginResult plResult; - // TODO Auto-generated method stub - try { - dbhelper.createdatabase(dbpath, src, callbackContext); -// plResult = new PluginResult(PluginResult.Status.OK); -// callbackContext.sendPluginResult(plResult); - //sendPluginResponse(200,"Db Copied", false, callbackContext); - } catch (Exception e) { + PluginResult plresult; -// plResult = new PluginResult(PluginResult.Status.ERROR, -// e.getMessage()); -// callbackContext.sendPluginResult(plResult); - sendPluginResponse(400, e.getMessage(), true, callbackContext); + // TODO Auto-generated method stub +// try { +// dbhelper.createdatabase(dbpath, src, callbackContext); +//// plResult = new PluginResult(PluginResult.Status.OK); +//// callbackContext.sendPluginResult(plResult); +// //sendPluginResponse(200,"Db Copied", false, callbackContext); +// } catch (Exception e) { +// +//// plResult = new PluginResult(PluginResult.Status.ERROR, +//// e.getMessage()); +//// callbackContext.sendPluginResult(plResult); +// sendPluginResponse(400, e.getMessage(), true, callbackContext); +// } + InputStream myInput = null; + JSONObject response = new JSONObject(); + try { + myInput = cordova.getActivity().getAssets().open("www/" + dbName); + + OutputStream myOutput = new FileOutputStream(dbpath); + byte[] buffer = new byte[1024]; + while ((myInput.read(buffer)) > -1) { + myOutput.write(buffer); + } + + myOutput.flush(); + myOutput.close(); + myInput.close(); + try { + response.put("message", "Db Copied"); + response.put("code", 200); + plresult = new PluginResult(PluginResult.Status.OK,response); + callbackContext.sendPluginResult(plresult); + } catch (JSONException err) { + throw new Error(err.getMessage()); + } + } catch (Exception e) { + Log.d("Cordova", "Try Error = "+e.getMessage()); + try { + dbhelper.createdatabase(dbpath, src, callbackContext); + } catch (Exception err) { + sendPluginResponse(400, err.getMessage(), true, callbackContext); + } } }