3

我已经按照本教程:http ://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/使我的应用程序从现有数据库中读取。

数据库已成功复制到/data/data/package/databases/,但我无法通过getReadableDatabase()方法访问它。

我收到此错误:attempt to re-open an already-closed object: SQLiteDatabase

DBHelper dbHelper = DBHelper.getInstance(this);
SQLiteDatabase db = dbHelper.getReadableDatabase();

访问数据库的唯一方法是使用此方法(教程中提供):

public SQLiteDatabase openDataBase() throws SQLException {
    // Open the database
    String myPath = DATABASE_PATH + DATABASE_NAME;
    return SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}

我不明白为什么getReadableDatabase不工作并且SQLiteDatabase.openDatabase是。

谢谢!


编辑

07-26 17:33:23.695: E/SQLiteLog(8091): (5) database is locked
07-26 17:33:23.715: E/SQLiteDatabase(8091): Failed to open database '/data/data/com.package/databases/DB'.
07-26 17:33:23.715: E/SQLiteDatabase(8091): android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:627)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:313)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:287)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:215)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at com.package.DBHelper.checkDataBase(DBHelper.java:274)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at com.package.DBHelper.createDataBase(DBHelper.java:241)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at com.package.DBHelper.onCreate(DBHelper.java:197)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at com.package.MainActivity.onResults(MainActivity.java:259)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at android.speech.SpeechRecognizer$InternalListener$1.handleMessage(SpeechRecognizer.java:442)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at android.os.Looper.loop(Looper.java:137)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at android.app.ActivityThread.main(ActivityThread.java:5227)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at java.lang.reflect.Method.invokeNative(Native Method)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at java.lang.reflect.Method.invoke(Method.java:511)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
07-26 17:33:23.715: E/SQLiteDatabase(8091):     at dalvik.system.NativeStart.main(Native Method)

.

07-26 16:29:10.883: E/AndroidRuntime(1909): FATAL EXCEPTION: main
07-26 16:29:10.883: E/AndroidRuntime(1909): java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.package.../databases/DB
07-26 16:29:10.883: E/AndroidRuntime(1909):     at    android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
07-26 16:29:10.883: E/AndroidRuntime(1909):     at    android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:520)
07-26 16:29:10.883: E/AndroidRuntime(1909):     at    android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:263)
07-26 16:29:10.883: E/AndroidRuntime(1909):     at    android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
07-26 16:29:10.883: E/AndroidRuntime(1909):     at com.dennis.voice.MainActivity.onResults(MainActivity.java:258)
07-26 16:29:10.883: E/AndroidRuntime(1909):     at android.speech.SpeechRecognizer$InternalListener$1.handleMessage(SpeechRecognizer.java:442)
07-26 16:29:10.883: E/AndroidRuntime(1909):     at    android.os.Handler.dispatchMessage(Handler.java:99)
07-26 16:29:10.883: E/AndroidRuntime(1909):     at android.os.Looper.loop(Looper.java:137)
07-26 16:29:10.883: E/AndroidRuntime(1909):     at android.app.ActivityThread.main(ActivityThread.java:5227)
07-26 16:29:10.883: E/AndroidRuntime(1909):     at java.lang.reflect.Method.invokeNative(Native Method)
07-26 16:29:10.883: E/AndroidRuntime(1909):     at java.lang.reflect.Method.invoke(Method.java:511)
07-26 16:29:10.883: E/AndroidRuntime(1909):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
07-26 16:29:10.883: E/AndroidRuntime(1909):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
07-26 16:29:10.883: E/AndroidRuntime(1909):     at dalvik.system.NativeStart.main(Native Method)
07-26 16:34:10.934: I/Process(1909): Sending signal. PID: 1909 SIG: 9

第 258 行:SQLiteDatabase db = dbHelper.getReadableDatabase();

4

0 回答 0