开发者

Android Select from attached database

I have an Android application where I have attached a database to my current database connection, but when I try to select from a table from the attached database I get an error reporting that there is no such table.

SQLiteDatabase dbc;
try {
    dbc = openOrCreateDatabase(DATABASE_RECOVERY,Context.MODE_PRIVATE,null);
    dbc.execSQL(TABLE_COMPLETE);
    dbc.execSQL("ATTACH DATABASE '" + DBRECOVERY_FULL_PATH + "' as MasterDb;");
    dbc.execSQL("SELECT * FROM MasterDb.properties;");
    db.close();
    dbc.close();
} catch(SQLException e) {
    Toast.makeText(getApplicationContext(), "SQLException: " + e.toString(), 
             Toast.LENGTH_开发者_如何学编程SHORT).show();
}

I have also tried:

dbc.execSQL("SELECT 'MasterDb', * FROM MasterDb.properties;");

But each time, it is reporting that there is no such table.


Using the query method from SQLiteDatabase can simplify your work, but according to your query try using dbc.execSQL("SELECT * FROM MasterDb");


Don't forget that to use an "attached" database you need to open it with this flag:

SQLiteDatabase.NO_LOCALIZED_COLLATORS

See SQLiteDatabase OpenDatabase.

To open the database you will need to run something like this:

String path = getDatabasePath("YourDbName").getAbsolutePath();
SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.CREATE_IF_NECESSARY + SQLiteDatabase.NO_LOCALIZED_COLLATORS);


I had this same issue. I forgot to copy the new DB to the file path before attaching it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜