ORMLite Issue with inserting tables from one database to another database on Android

点点圈 提交于 2019-12-05 18:11:48

I'm a coworker of the author of this question, and I managed to find a way to avoid this problem. I figured I'd post it here in case anyone else is stumbling across the same issue.

Android devices with that very specific OS version seemed to have a problem with this sequence of Ormlite commands:

DatabaseConnection con = null;
con = conSrc.getReadWriteConnection();
con.executeStatement("attach database '" + localDatabase.getAbsolutePath() + "' as '" + localDb + "'", DatabaseConnection.DEFAULT_RESULT_FLAGS);
con.executeStatement("attach database '" + downloadedDatabase.getAbsolutePath() + "' as '" + remoteDb + "'", DatabaseConnection.DEFAULT_RESULT_FLAGS);

I don't know if this is caused by the version of SQLite3 that comes preinstalled on these devices, or by something else.

Now we could avoid this because we could (luckily) avoid using Ormlite in this situation altogether. The SQLite statement "ATTACH DATABASE" does not need to be executed on a DatabaseConnection instance - it can be executed on the local database file. So instead of the former block, only this statement is necessary:

db.execSQL("ATTACH DATABASE '" + tempDb.getAbsolutePath() + "' AS '" + remoteDb + "'");

where db is the SQLiteDatabase instance of the local database. The SQLite documentation states that:

"The ATTACH DATABASE statement adds another database file to the current database connection."

which is why no DatabaseConnection instance is needed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!