mirror of
https://gitee.com/shuto/cordova-plugin-dbcopy.git
synced 2024-10-06 09:42:06 +08:00
Fix DB Copy Issue #52
This commit is contained in:
parent
523e3c6ae1
commit
c7507bbbfb
@ -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,12 +23,10 @@
|
||||
"database copy",
|
||||
"sqlite database copy"
|
||||
],
|
||||
"engines": [
|
||||
{
|
||||
"engines": [{
|
||||
"name": "cordova",
|
||||
"version": ">=3.0.0"
|
||||
}
|
||||
],
|
||||
}],
|
||||
"author": "Rahul Pandey",
|
||||
"license": "Apache 2.0",
|
||||
"bugs": {
|
||||
|
@ -1,6 +1,5 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<plugin id="cordova-plugin-dbcopy"
|
||||
version="2.0.0"
|
||||
<plugin id="cordova-plugin-dbcopy" version="2.1.0"
|
||||
xmlns="http://apache.org/cordova/ns/plugins/1.0"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
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) {
|
||||
//
|
||||
//// 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);
|
||||
// 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);
|
||||
} catch (Exception err) {
|
||||
sendPluginResponse(400, err.getMessage(), true, callbackContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user