Laura
Laura

Reputation: 2803

Android Detach Database

I use in my application 2 databases. I have a main database which is used the most and at some point I need the 2nd database. So, in order to use it I attached it to the main database using this method:

public void attachDb(String dbName, String dbAlias){
    String dbPath = context.getDatabasePath(dbName).getAbsolutePath();
    myDataBase.execSQL("attach database '" + dbPath + "' as " + dbAlias);
}

Now my problem is that I want to detach the 2nd database when I exit the screen and I get a Force Close. I tried to detach it using this code:

 public void detachDatabase(String dbName){
    String dbPath = context.getDatabasePath(dbName).getAbsolutePath();
    myDataBase.execSQL("detach database '" + dbPath + "'");
}

The error log says this:

05-13 11:48:23.173: ERROR/AndroidRuntime(7338): FATAL EXCEPTION: Thread-145
        android.database.sqlite.SQLiteException: error code 1: SQL logic error or missing database
        at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
        at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:92)
        at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1899)
        at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1839)
        at com.example.DBManagerBase.detachDatabase(DBManagerBase.java:168)
        at com.example.run(MyClass.java:233)
        at java.lang.Thread.run(Thread.java:856)

I don't know what the problem could be.

Upvotes: 2

Views: 1331

Answers (1)

Andre Rocha
Andre Rocha

Reputation: 988

use only myDataBase.execSQL("detach " + dbAlias)
You don't use the database's path+name, just the alias you gave.

Upvotes: 7

Related Questions