以下代码片段应该从 SQLite 表中删除重复项:
public int removeDuplicates(String table, String column, String idcolumn) {
if (mDB.isReadOnly()) return -1;
String idcol = idcolumn == null ? "_id" : idcolumn;
String sql = String.format(Locale.US, "DELETE from %s where %s in " + "(select %s from %s JOIN "
+ "(select min(%s) as id_keep,%s,count(*) as dups from %s group by %s) AS A "
+ "ON A.%s=%s.%s where %s<>id_keep)", table, idcol, idcol, table, idcol, column, table, column, column,
table, column, idcol);
SQLiteStatement sqlcmd = mDB.compileStatement(sql);
int result = sqlcmd.executeUpdateDelete();
return result;
}
问题是 aSQLiteReadOnlyDatabaseException
被抛出,即使它通过了!db.isReadOnly()
测试。
我认为这个问题与这个问题无关,因为所有内容都是针对 ICS 编译的;和其他应用程序(使用 ContentProvider 驱动查询)能够写入数据库。
数据库位于 SD 卡上的公用文件夹中。
堆栈跟踪是:
11-02 14:48:24.596 E/AndroidRuntime(12324): android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database
11-02 14:48:24.596 E/AndroidRuntime(12324): at android.database.sqlite.SQLiteStatement.native_executeSql(Native Method)
11-02 14:48:24.596 E/AndroidRuntime(12324): at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:90)
11-02 14:48:24.596 E/AndroidRuntime(12324): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1899)
11-02 14:48:24.596 E/AndroidRuntime(12324): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1839)
11-02 14:48:24.596 E/AndroidRuntime(12324): at android.database.sqlite.SQLiteDatabase.beginTransaction(SQLiteDatabase.java:661)
11-02 14:48:24.596 E/AndroidRuntime(12324): at android.database.sqlite.SQLiteDatabase.beginTransactionNonExclusive(SQLiteDatabase.java:576)
11-02 14:48:24.596 E/AndroidRuntime(12324): at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:247)
11-02 14:48:24.596 E/AndroidRuntime(12324): at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:84)
11-02 14:48:24.596 E/AndroidRuntime(12324): at za.co.nimbus.druids.DatabaseTools.removeDuplicates(DatabaseTools.java:139)
有任何想法吗?