SQLiteDatabase在可写数据库上抛出SQLiteReadOnlyDatabaseException

时间:2012-11-02 12:58:12

标签: android android-sqlite

以下代码段应该从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;
}

问题是SQLiteReadOnlyDatabaseException被抛出,即使它通过了!db.isReadOnly()测试。

我不认为这个问题与this question有关,因为一切都是针对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)

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

为了写入存储在SD上的数据库,您需要具有写入外部存储器的权限。只需将以下内容添加到清单中:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />