`

android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a read

阅读更多
android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database

在开发一个app的过程中,使用了Provider,通过install 的方式将APP运行在机器上,再通过setting清除该APP再运行APP,一切仍正常,但若通过系统签名将app push到system/app上,再在setting上清除该APP的数据,再运行就发生了android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database。 同样的code为什么通过不同的方式就会有问题呢?我纠结了好久,先从代码去分析mOpenHelper.getWritableDatabase()它会检查数据库是否存在,如果不存在就会创建。但是我到/data/data/com...../databases/下却没有DB,而mOpenHelper.getWritableDatabase()返回DB不为空。再insert的时候就会发生attempt to write a readonly database!为何明明数据库不存在了,但仍返回不为空的DB??

接着我下载了一个Provider Demo做了系统签名并push 到system/app上,按照那测试路径,发现仍正常!太不可思议了!!

然后做了很多尝试,都没有找到原因...

为了赶出code,采取了一种很暴力的方式解决,就是
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
  File database=new File("/data/data/com.lewa.updater/databases");
        if(!database.exists()&&db!=null){
        	db.close();
        	db = mOpenHelper.getWritableDatabase();
        }


上了code接着解决另外一个关于写文件需要权限的bug,这也是很难啃的一个,折腾了一晚上都没有解决,但无意让我发现了上一个bug的原因,发现了我的app和provide demo 的app差别在于这里
    <application
        android:name="com...."
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/Theme.Style"
        android:persistent="true" >

这行android:persistent="true",我把这行加入到demo里面,果真,android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database发生了,然后查阅了http://www.cnblogs.com/perseus/archive/2012/01/11/2319660.html这文章,就解释了上面那一切了。

oh! My God!
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics