我正在编写一个使用ContentProvider
来获取其数据库的SqlOpenHelper
。我刚刚对数据库进行了一些重大更改,因此我将数据库的版本增加了一个。
发生了这种情况:
02-13 15:36:15.668: E/AndroidRuntime(15917): FATAL EXCEPTION: AsyncTask #3
02-13 15:36:15.668: E/AndroidRuntime(15917): java.lang.RuntimeException: An error occured while executing doInBackground()
02-13 15:36:15.668: E/AndroidRuntime(15917): at android.os.AsyncTask$3.done(AsyncTask.java:278)
02-13 15:36:15.668: E/AndroidRuntime(15917): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-13 15:36:15.668: E/AndroidRuntime(15917): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-13 15:36:15.668: E/AndroidRuntime(15917): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-13 15:36:15.668: E/AndroidRuntime(15917): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-13 15:36:15.668: E/AndroidRuntime(15917): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-13 15:36:15.668: E/AndroidRuntime(15917): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-13 15:36:15.668: E/AndroidRuntime(15917): at java.lang.Thread.run(Thread.java:864)
02-13 15:36:15.668: E/AndroidRuntime(15917): Caused by: android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 2 to 3: /data/data/com.company.anothercompany.anotherothercompany/databases/twocompanies
02-13 15:36:15.668: E/AndroidRuntime(15917): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:262)
02-13 15:36:15.668: E/AndroidRuntime(15917): at com.somo.vertu.provider.FerrariVertuContentProvider.query(FerrariVertuContentProvider.java:94)
02-13 15:36:15.668: E/AndroidRuntime(15917): at android.content.ContentProvider$Transport.query(ContentProvider.java:189)
02-13 15:36:15.668: E/AndroidRuntime(15917): at android.content.ContentResolver.query(ContentResolver.java:315)
02-13 15:36:15.668: E/AndroidRuntime(15917): at android.content.CursorLoader.loadInBackground(CursorLoader.java:56)
02-13 15:36:15.668: E/AndroidRuntime(15917): at android.content.CursorLoader.loadInBackground(CursorLoader.java:42)
02-13 15:36:15.668: E/AndroidRuntime(15917): at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:255)
02-13 15:36:15.668: E/AndroidRuntime(15917): at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:66)
02-13 15:36:15.668: E/AndroidRuntime(15917): at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:55)
02-13 15:36:15.668: E/AndroidRuntime(15917): at android.os.AsyncTask$2.call(AsyncTask.java:264)
02-13 15:36:15.668: E/AndroidRuntime(15917): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-13 15:36:15.668: E/AndroidRuntime(15917): ... 4 more
我从getReadableDatabase()
函数调用query(...)
,因为我正在阅读,而不是写作。在这种情况下,onUpgrade()
意味着什么都没有返回,但我正在处理它。如何在不使用getWritableDatabase(...)
方法中调用ContentProvider#onCreate()
的情况下触发可写数据库的升级?或者这是最好的事情吗?
答案 0 :(得分:1)
<强>重写强>
我看到了三个选项:
getWritableDatabase()
。getReadableDatabase()
内升级,如果是这样的话,请拨打getWritableDatabase()
。