0

Someone guide me plzzzz. I have a basic android-sqlite-database app for doing CRUD operations. It works fine when I do operations on /data/data/package-name/mydatabase.db. But now I have to embed the database file into the app. I have put the .db file in assets folder and written this code. But facing continuous problems as described below.... My DatabaseHandler class is as follows....

private static final String DATABASE_PATH = "com.example.database_abc/assets/contactsDB.db" ; 
private static final String DATABASE_NAME = "contactsDB";
private  SQLiteDatabase myDatabase ;
private final Context myContext ;

public DatabaseHandler3(Context context){
    super(context, DATABASE_NAME, null, 1);
    this.myContext = context ;

}

public boolean checkDatabase(){
    SQLiteDatabase checkDB = null ;
    try{
        checkDB = SQLiteDatabase.openDatabase( DATABASE_PATH , null, SQLiteDatabase.OPEN_READWRITE);
    }catch(SQLException ioex){
        throw new Error("Database Not Found..........................");
    }

    if(checkDB != null){
        checkDB.close();
    }

    return checkDB != null ? true : false ;
}// End of checkDataBase()


public void openDataBase() throws SQLException{

    //Open the database
    String myPath = DATABASE_PATH ;
    myDatabase = SQLiteDatabase.openDatabase(DATABASE_PATH, null, SQLiteDatabase.OPEN_READWRITE);

   }

@Override
public void onCreate(SQLiteDatabase db){

}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2){

} 

My Logcat is as follows..

12-13 17:24:29.244: E/SQLiteLog(5563): (14) cannot open file at line 30176 of [00bb9c9ce4]
12-13 17:24:29.244: E/SQLiteLog(5563): (14) os_unix.c:30176: (2) open(//com.example.database_abc/assets/contactsDB.db3) - 
12-13 17:24:29.384: E/SQLiteDatabase(5563): Failed to open database 'com.example.database_abc/assets/contactsDB.db3'.
12-13 17:24:29.384: E/SQLiteDatabase(5563): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at com.example.database_abc.DatabaseHandler3.checkDatabase(DatabaseHandler3.java:27)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at com.example.database_abc.MainActivity.onButtonClick(MainActivity.java:55)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at java.lang.reflect.Method.invokeNative(Native Method)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at java.lang.reflect.Method.invoke(Method.java:511)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.view.View$1.onClick(View.java:3592)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.view.View.performClick(View.java:4202)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.view.View$PerformClick.run(View.java:17340)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.os.Handler.handleCallback(Handler.java:725)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.os.Looper.loop(Looper.java:137)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at java.lang.reflect.Method.invokeNative(Native Method)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at java.lang.reflect.Method.invoke(Method.java:511)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-13 17:24:29.384: E/SQLiteDatabase(5563):     at dalvik.system.NativeStart.main(Native Method)
12-13 17:24:29.384: D/AndroidRuntime(5563): Shutting down VM
12-13 17:24:29.394: W/dalvikvm(5563): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-13 17:24:29.504: E/AndroidRuntime(5563): FATAL EXCEPTION: main
12-13 17:24:29.504: E/AndroidRuntime(5563): java.lang.IllegalStateException: Could not execute method of the activity
12-13 17:24:29.504: E/AndroidRuntime(5563):     at android.view.View$1.onClick(View.java:3597)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at android.view.View.performClick(View.java:4202)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at android.view.View$PerformClick.run(View.java:17340)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at android.os.Handler.handleCallback(Handler.java:725)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at android.os.Looper.loop(Looper.java:137)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at java.lang.reflect.Method.invokeNative(Native Method)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at java.lang.reflect.Method.invoke(Method.java:511)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at dalvik.system.NativeStart.main(Native Method)
12-13 17:24:29.504: E/AndroidRuntime(5563): Caused by: java.lang.reflect.InvocationTargetException
12-13 17:24:29.504: E/AndroidRuntime(5563):     at java.lang.reflect.Method.invokeNative(Native Method)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at java.lang.reflect.Method.invoke(Method.java:511)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at android.view.View$1.onClick(View.java:3592)
12-13 17:24:29.504: E/AndroidRuntime(5563):     ... 11 more
12-13 17:24:29.504: E/AndroidRuntime(5563): Caused by: java.lang.Error: Database Not Found..........................
12-13 17:24:29.504: E/AndroidRuntime(5563):     at com.example.database_abc.DatabaseHandler3.checkDatabase(DatabaseHandler3.java:29)
12-13 17:24:29.504: E/AndroidRuntime(5563):     at com.example.database_abc.MainActivity.onButtonClick(MainActivity.java:55)
12-13 17:24:29.504: E/AndroidRuntime(5563):     ... 14 more
1
  • why not just create the DB when your app starts, and include any default data in the scripts you're executing? Commented Dec 13, 2012 at 18:05

3 Answers 3

1

Not sure how you're creating the database, but using something like the Firefox SQLiteManager to create, then pasting into the assets directory has always worked fine for me. The other reason could be version incompatibilities, or something has simple as the extension (db3 vs. db or sqlite).

Sign up to request clarification or add additional context in comments.

3 Comments

I have previously created the database, perform the CRUD operations (when file was in "data/data/package-name/database/" directory. But I want to use the embedded database. So I pull the same file into assets and changed the code, but it did not work. It is the same file as that one.
Hm, then I'm afraid I don't understand what you want. "Embedded database", like, on the device? A database stored in another?
I just want to add database into my app means when I install it on android phone it will have its own database to store the data. If Im doing something wrong, please tell me
0

Here is a tutorial that I used to create my apps which use a read-only sqlite database:

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

It gives all the code needed to move a database packaged in your asset folder to its final resting place in "/data/data/XX.XXX.XX/databases" location.

Also give this link a read if your database is large:

http://ponystyle.com/blog/2010/03/26/dealing-with-asset-compression-in-android-apps/

1 Comment

I have used this tutorial but it updates the same .db file i.e. "data/data/packagename/databases/mydatabase.db". My assets/mydatabases.db file was not updating. Dear, has it worked well for read-only purpose???
0

Donnot what's wrong with this, but i think you should better try this

SQLiteDatabase.openOrCreateDatabase(path, factory);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.