Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,733,372 Members 39,678 Now Online
XDA Developers Android and Mobile Development Forum

[Q] Loading a database along with apk file

Tip us?
 
achtuhan M
Old
#1  
Junior Member - OP
Thanks Meter 0
Posts: 1
Join Date: May 2014
Default [Q] Loading a database along with apk file

I want to deploy a database along with my apk , so i modified my sqliteopenhelper as follows:

Code:
    public class openingclass extends SQLiteOpenHelper  
        {  
            public openingclass(Context c) {  
                super(c,Db_NAME, null, DB_VERSION);  
            }  
             public void createDataBase() {  
      
                    boolean dbExist;  
                    try {  
      
                         dbExist = checkDataBase();  
      
      
                    } catch (SQLiteException e) {  
      
                        e.printStackTrace();  
                        throw new Error("database dose not exist");  
      
                    }  
      
                    if(dbExist){  
                    //do nothing - database already exist  
                    }else{  
      
                        try {  
      
                            copyDataBase();  
      
      
                        } catch (IOException e) {  
      
                            e.printStackTrace();  
                            throw new Error("Error copying database");  
      
                        }  
                //By calling this method and empty database will be created into the default system path  
                //of your application so we are gonna be able to overwrite that database with our database.  
                    this.getReadableDatabase();  
      
      
                }  
      
                }  
      
                /** 
                  * Check if the database already exist to avoid re-copying the file each time you open the application. 
                  * @return true if it exists, false if it doesn't 
                  */  
                private boolean checkDataBase(){  
      
                SQLiteDatabase checkDB = null;  
      
                try{  
                    String myPath = DB_PATH +"/"+ Db_NAME;  
      
                    checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);  
                }catch(SQLiteException e){  
      
                //database does't exist yet.  
                    throw new Error("database does't exist yet.");  
      
                }  
      
                if(checkDB != null){  
      
                checkDB.close();  
      
                }  
      
                return checkDB != null ? true : false;  
                }  
      
                /** 
                  * Copies your database from your local assets-folder to the just created empty database in the 
                  * system folder, from where it can be accessed and handled. 
                  * This is done by transfering bytestream. 
                  * */  
                private void copyDataBase() throws IOException{  
      
      
      
                        //copyDataBase();  
                        //Open your local db as the input stream  
                        InputStream myInput = c1.getAssets().open(Db_NAME);  
      
                        // Path to the just created empty db  
                        String outFileName = DB_PATH +"/"+ Db_NAME;  
                        File databaseFile = new File(DB_PATH);  
                         // check if databases folder exists, if not create one and its subfolders  
                        if (!databaseFile.exists()){  
                            databaseFile.mkdir();  
                        }  
      
                        //Open the empty db as the output stream  
                        OutputStream myOutput = new FileOutputStream(outFileName);  
      
                        //transfer bytes from the inputfile to the outputfile  
                        byte[] buffer = new byte[1024];  
                        int length;  
                        while ((length = myInput.read(buffer))>0){  
                        myOutput.write(buffer, 0, length);  
                        }  
      
                        //Close the streams  
                        myOutput.flush();  
                        myOutput.close();  
                        myInput.close();  
      
      
      
                }  
      
      
      
                @Override  
                public synchronized void close() {  
      
                    if(myDataBase != null)  
                    myDataBase.close();  
      
                    super.close();  
      
                }  
      
            @Override  
            public void onCreate(SQLiteDatabase arg0) {  
                String S = "create table " +  
                        TABLE_NAME +  
                        " (" +  
                        TABLE_COL_MAIL + " text primary key," +  
                        TABLE_COL_NAME + " text," +  
                        TABLE_COL_PASS + " text," +  
                        TABLE_COL_PHO + " text," +  
                        TABLE_COL_ADD + " text," +  
                        TABLE_COL_GEN + " text," +  
                        TABLE_COL_DOB + " text" +  
                        ");";  
                arg0.execSQL(S);  
      
                String S1 = "create table " +  
                        SECOND_TABLE_NAME +  
                        " (" +  
                        TABLE_COL_USER + " text," +  
                        TABLE_COL_PRODUCT + " text," +  
                        TABLE_COL_QUANTITY + " integer" +  
                        ");";  
                arg0.execSQL(S1);  
      
      
      
      
            }  
      
            @Override  
            public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {  
                // TODO Auto-generated method stub  
      
            }
The problem which people on other forums tell me is to do with the path. I have hard coded the path in my datamanager class as follows:

Code:
    private final String DB_PATH="/data/data/com.example.shopkart/databases";  
        private final String Db_NAME = "dbshopkart.db";
I get the following error in my logcat on running on the device:

Code:
    05-02 16:20:16.039: E/AndroidRuntime(9916): FATAL EXCEPTION: main  
    05-02 16:20:16.039: E/AndroidRuntime(9916): java.lang.Error: database does't exist yet.  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at com.example.shopkart.datamanager$openingclass.checkDataBase(datamanager.java:112)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at com.example.shopkart.datamanager$openingclass.createDataBase(datamanager.java:63)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at com.example.shopkart.datamanager$openingclass.<init>(datamanager.java:56)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at com.example.shopkart.datamanager.<init>(datamanager.java:48)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at com.example.shopkart.MainActivity.onCreate(MainActivity.java:50)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at android.os.Handler.dispatchMessage(Handler.java:99)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at android.os.Looper.loop(Looper.java:130)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at android.app.ActivityThread.main(ActivityThread.java:3689)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at java.lang.reflect.Method.invokeNative(Native Method)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at java.lang.reflect.Method.invoke(Method.java:507)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at dalvik.system.NativeStart.main(Native Method)  
    05-02 16:25:32.835: E/Database(10275): sqlite3_open_v2("/data/data/com.example.shopkart/databases/dbshopkart.db", &handle, 1, NULL) failed  
    05-02 16:25:32.835: W/dalvikvm(10275): threadid=1: thread exiting with uncaught exception (group=0x40015578)  
    05-02 16:25:32.835: E/AndroidRuntime(10275): FATAL EXCEPTION: main  
    05-02 16:25:32.835: E/AndroidRuntime(10275): java.lang.Error: database does't exist yet.  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at com.example.shopkart.datamanager$openingclass.checkDataBase(datamanager.java:112)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at com.example.shopkart.datamanager$openingclass.createDataBase(datamanager.java:63)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at com.example.shopkart.datamanager$openingclass.<init>(datamanager.java:56)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at com.example.shopkart.datamanager.<init>(datamanager.java:48)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at com.example.shopkart.MainActivity.onCreate(MainActivity.java:50)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at android.os.Handler.dispatchMessage(Handler.java:99)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at android.os.Looper.loop(Looper.java:130)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at android.app.ActivityThread.main(ActivityThread.java:3689)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at java.lang.reflect.Method.invokeNative(Native Method)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at java.lang.reflect.Method.invoke(Method.java:507)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at dalvik.system.NativeStart.main(Native Method)
The database seems to be not found.I have even put the database in the assets folder.Please help!
 
OkieKokie
Old
#2  
Junior Member
Thanks Meter 3
Posts: 13
Join Date: Jul 2014
Quote:
Originally Posted by achtuhan M View Post
I want to deploy a database along with my apk , so i modified my sqliteopenhelper as follows:

Code:
    public class openingclass extends SQLiteOpenHelper  
        {  
            public openingclass(Context c) {  
                super(c,Db_NAME, null, DB_VERSION);  
            }  
             public void createDataBase() {  
      
                    boolean dbExist;  
                    try {  
      
                         dbExist = checkDataBase();  
      
      
                    } catch (SQLiteException e) {  
      
                        e.printStackTrace();  
                        throw new Error("database dose not exist");  
      
                    }  
      
                    if(dbExist){  
                    //do nothing - database already exist  
                    }else{  
      
                        try {  
      
                            copyDataBase();  
      
      
                        } catch (IOException e) {  
      
                            e.printStackTrace();  
                            throw new Error("Error copying database");  
      
                        }  
                //By calling this method and empty database will be created into the default system path  
                //of your application so we are gonna be able to overwrite that database with our database.  
                    this.getReadableDatabase();  
      
      
                }  
      
                }  
      
                /** 
                  * Check if the database already exist to avoid re-copying the file each time you open the application. 
                  * @return true if it exists, false if it doesn't 
                  */  
                private boolean checkDataBase(){  
      
                SQLiteDatabase checkDB = null;  
      
                try{  
                    String myPath = DB_PATH +"/"+ Db_NAME;  
      
                    checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);  
                }catch(SQLiteException e){  
      
                //database does't exist yet.  
                    throw new Error("database does't exist yet.");  
      
                }  
      
                if(checkDB != null){  
      
                checkDB.close();  
      
                }  
      
                return checkDB != null ? true : false;  
                }  
      
                /** 
                  * Copies your database from your local assets-folder to the just created empty database in the 
                  * system folder, from where it can be accessed and handled. 
                  * This is done by transfering bytestream. 
                  * */  
                private void copyDataBase() throws IOException{  
      
      
      
                        //copyDataBase();  
                        //Open your local db as the input stream  
                        InputStream myInput = c1.getAssets().open(Db_NAME);  
      
                        // Path to the just created empty db  
                        String outFileName = DB_PATH +"/"+ Db_NAME;  
                        File databaseFile = new File(DB_PATH);  
                         // check if databases folder exists, if not create one and its subfolders  
                        if (!databaseFile.exists()){  
                            databaseFile.mkdir();  
                        }  
      
                        //Open the empty db as the output stream  
                        OutputStream myOutput = new FileOutputStream(outFileName);  
      
                        //transfer bytes from the inputfile to the outputfile  
                        byte[] buffer = new byte[1024];  
                        int length;  
                        while ((length = myInput.read(buffer))>0){  
                        myOutput.write(buffer, 0, length);  
                        }  
      
                        //Close the streams  
                        myOutput.flush();  
                        myOutput.close();  
                        myInput.close();  
      
      
      
                }  
      
      
      
                @Override  
                public synchronized void close() {  
      
                    if(myDataBase != null)  
                    myDataBase.close();  
      
                    super.close();  
      
                }  
      
            @Override  
            public void onCreate(SQLiteDatabase arg0) {  
                String S = "create table " +  
                        TABLE_NAME +  
                        " (" +  
                        TABLE_COL_MAIL + " text primary key," +  
                        TABLE_COL_NAME + " text," +  
                        TABLE_COL_PASS + " text," +  
                        TABLE_COL_PHO + " text," +  
                        TABLE_COL_ADD + " text," +  
                        TABLE_COL_GEN + " text," +  
                        TABLE_COL_DOB + " text" +  
                        ");";  
                arg0.execSQL(S);  
      
                String S1 = "create table " +  
                        SECOND_TABLE_NAME +  
                        " (" +  
                        TABLE_COL_USER + " text," +  
                        TABLE_COL_PRODUCT + " text," +  
                        TABLE_COL_QUANTITY + " integer" +  
                        ");";  
                arg0.execSQL(S1);  
      
      
      
      
            }  
      
            @Override  
            public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {  
                // TODO Auto-generated method stub  
      
            }
The problem which people on other forums tell me is to do with the path. I have hard coded the path in my datamanager class as follows:

Code:
    private final String DB_PATH="/data/data/com.example.shopkart/databases";  
        private final String Db_NAME = "dbshopkart.db";
I get the following error in my logcat on running on the device:

Code:
    05-02 16:20:16.039: E/AndroidRuntime(9916): FATAL EXCEPTION: main  
    05-02 16:20:16.039: E/AndroidRuntime(9916): java.lang.Error: database does't exist yet.  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at com.example.shopkart.datamanager$openingclass.checkDataBase(datamanager.java:112)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at com.example.shopkart.datamanager$openingclass.createDataBase(datamanager.java:63)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at com.example.shopkart.datamanager$openingclass.<init>(datamanager.java:56)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at com.example.shopkart.datamanager.<init>(datamanager.java:48)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at com.example.shopkart.MainActivity.onCreate(MainActivity.java:50)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at android.os.Handler.dispatchMessage(Handler.java:99)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at android.os.Looper.loop(Looper.java:130)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at android.app.ActivityThread.main(ActivityThread.java:3689)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at java.lang.reflect.Method.invokeNative(Native Method)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at java.lang.reflect.Method.invoke(Method.java:507)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)  
    05-02 16:20:16.039: E/AndroidRuntime(9916):     at dalvik.system.NativeStart.main(Native Method)  
    05-02 16:25:32.835: E/Database(10275): sqlite3_open_v2("/data/data/com.example.shopkart/databases/dbshopkart.db", &handle, 1, NULL) failed  
    05-02 16:25:32.835: W/dalvikvm(10275): threadid=1: thread exiting with uncaught exception (group=0x40015578)  
    05-02 16:25:32.835: E/AndroidRuntime(10275): FATAL EXCEPTION: main  
    05-02 16:25:32.835: E/AndroidRuntime(10275): java.lang.Error: database does't exist yet.  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at com.example.shopkart.datamanager$openingclass.checkDataBase(datamanager.java:112)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at com.example.shopkart.datamanager$openingclass.createDataBase(datamanager.java:63)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at com.example.shopkart.datamanager$openingclass.<init>(datamanager.java:56)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at com.example.shopkart.datamanager.<init>(datamanager.java:48)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at com.example.shopkart.MainActivity.onCreate(MainActivity.java:50)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at android.os.Handler.dispatchMessage(Handler.java:99)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at android.os.Looper.loop(Looper.java:130)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at android.app.ActivityThread.main(ActivityThread.java:3689)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at java.lang.reflect.Method.invokeNative(Native Method)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at java.lang.reflect.Method.invoke(Method.java:507)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)  
    05-02 16:25:32.835: E/AndroidRuntime(10275):    at dalvik.system.NativeStart.main(Native Method)
The database seems to be not found.I have even put the database in the assets folder.Please help!
Have u created the android metadata table within the database?
 
spartan.coding
Old
#3  
Junior Member
Thanks Meter 0
Posts: 1
Join Date: Jul 2014
Database DB File path

Here is my copyDatabase method. I used a File object to store the DB path. I was having the same issue.
Code:
	
    public void copyDatabase(Context context) {
		final File DB_PATH = context.getDatabasePath("phrasedb");		
		Log.i("Database",
	               "New database is being copied to device!");
	    byte[] buffer = new byte[1024];
	    OutputStream myOutput = null;
	    int length;
	    // Open your local db as the input stream
	    InputStream myInput = null;
	    File databaseFile = new File(DB_PATH.getPath());
	    Log.i("DATABASE", "File path " + DB_PATH.getPath());
	    if(!databaseFile.exists()){
	       	databaseFile.mkdir();
	       	Log.i("File", "File didn't exist...making dir!");
	    }
	    try
	    {
	        myInput = context.getAssets().open("phrasedb");	            
	        // transfer bytes from the inputfile to the
	        // outputfile	           
	        myOutput = new FileOutputStream(DB_PATH.getPath());	
	        Log.i("DATABASE", "Destination path " + DB_PATH.getPath());
	        while((length = myInput.read(buffer)) > 0)
	        {
	            myOutput.write(buffer, 0, length);
	            Log.i("Database", "Copying...");
	        }
	        myOutput.close();
	        myOutput.flush();
	        myInput.close();
	        Log.i("Database",
	               "New database has been copied to device!");

	    }
	    catch(IOException e)
	    {
	        e.printStackTrace();
	        Log.e("exception", "Exception thrown while copying db!");
	    }
	}
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes