我有一个应用程序从 sqlite 数据库浏览器读取数据库,然后复制数据库并继续。但是,每次我在 sqlite 数据库浏览器中更改数据库时,我都必须卸载并重新安装应用程序才能再次复制它。(这只是为了大学作业)在第一次启动时它总是崩溃,但在第二次启动时就可以了。有没有办法避免这种崩溃?
这是我的一些代码:
public DbH(Context context) throws IOException {
super(context, DB_NAME, null, 1);
this.mycontext = context;
}
public void createdatabase() throws IOException {
boolean dbexist = checkdatabase();
if (dbexist) {
System.out.println(" Database exists.");
} else {
this.getReadableDatabase();
try {
copydatabase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkdatabase() {
// SQLiteDatabase checkdb = null;
boolean checkdb = false;
try {
String myPath = DB_PATH + DB_NAME;
File dbfile = new File(myPath);
checkdb = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE) != null;
checkdb = dbfile.exists();
} catch (SQLiteException e) {
System.out.println("Database doesn't exist");
}
return checkdb;
}
private void copydatabase() throws IOException {
InputStream myinput = mycontext.getAssets().open(DB_NAME);
String outfilename = DB_PATH + DB_NAME;
OutputStream myoutput = new FileOutputStream(outfilename);
byte[] buffer = new byte[1024];
int length;
while ((length = myinput.read(buffer)) > 0) {
myoutput.write(buffer, 0, length);
}
myoutput.flush();
myoutput.close();
myinput.close();
}
public void opendatabase() throws SQLException {
// Open the database
String mypath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(mypath, null,
SQLiteDatabase.OPEN_READONLY);
}
public synchronized void close() {
if (myDataBase != null) {
myDataBase.close();
}
super.close();
}
以下是 Logcat 中的错误
04-01 22:24:55.909: E/SqliteDatabaseCpp(25335): DB info: sqlite3_open_v2("/data/data/com.example.databasebrowser/databases , key = T", &handle, 2, NULL) failed
04-01 22:24:55.909: E/SqliteDatabaseCpp(25335): DB info: errno = 2, errno message = No such file or directory
04-01 22:24:55.949: E/SQLiteDatabase(25335): Failed to open the database. closing it.
04-01 22:24:55.949: E/SQLiteDatabase(25335): android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
04-01 22:24:55.949: E/SQLiteDatabase(25335): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
04-01 22:24:55.949: E/SQLiteDatabase(25335): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1093)
04-01 22:24:55.949: E/SQLiteDatabase(25335): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1050)
04-01 22:24:55.949: E/SQLiteDatabase(25335): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1026)
04-01 22:24:55.949: E/SQLiteDatabase(25335): at com.example.databasebrowser.DbH.checkdatabase(DbH.java:60)
04-01 22:24:55.949: E/SQLiteDatabase(25335): at com.example.databasebrowser.DbH.createdatabase(DbH.java:41)
04-01 22:24:55.949: E/SQLiteDatabase(25335): at com.example.databasebrowser.MyActivity.validateLogin(MyActivity.java:99)
04-01 22:24:55.949: E/SQLiteDatabase(25335): at com.example.databasebrowser.MyActivity.onClick(MyActivity.java:66)
04-01 22:24:55.949: E/SQLiteDatabase(25335): at android.view.View.performClick(View.java:3538)
04-01 22:24:55.949: E/SQLiteDatabase(25335): at android.view.View$PerformClick.run(View.java:14330)
04-01 22:24:55.949: E/SQLiteDatabase(25335): at android.os.Handler.handleCallback(Handler.java:608)
04-01 22:24:55.949: E/SQLiteDatabase(25335): at android.os.Handler.dispatchMessage(Handler.java:92)
04-01 22:24:55.949: E/SQLiteDatabase(25335): at android.os.Looper.loop(Looper.java:156)
04-01 22:24:55.949: E/SQLiteDatabase(25335): at android.app.ActivityThread.main(ActivityThread.java:4977)
04-01 22:24:55.949: E/SQLiteDatabase(25335): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 22:24:55.949: E/SQLiteDatabase(25335): at java.lang.reflect.Method.invoke(Method.java:511)
04-01 22:24:55.949: E/SQLiteDatabase(25335): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-01 22:24:55.949: E/SQLiteDatabase(25335): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-01 22:24:55.949: E/SQLiteDatabase(25335): at dalvik.system.NativeStart.main(Native Method)
04-01 22:24:56.450: E/SqliteDatabaseCpp(25335): SELECT locale FROM android_metadata failed
04-01 22:24:56.470: E/SQLiteDatabase(25335): Failed to open the database. closing it.
04-01 22:24:56.470: E/SQLiteDatabase(25335): android.database.sqlite.SQLiteException: no such table: android_metadata
04-01 22:24:56.470: E/SQLiteDatabase(25335): at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)
04-01 22:24:56.470: E/SQLiteDatabase(25335): at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:2186)
04-01 22:24:56.470: E/SQLiteDatabase(25335): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1094)
04-01 22:24:56.470: E/SQLiteDatabase(25335): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1050)
04-01 22:24:56.470: E/SQLiteDatabase(25335): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1026)
04-01 22:24:56.470: E/SQLiteDatabase(25335): at com.example.databasebrowser.DbH.opendatabase(DbH.java:98)
04-01 22:24:56.470: E/SQLiteDatabase(25335): at com.example.databasebrowser.MyActivity.validateLogin(MyActivity.java:105)
04-01 22:24:56.470: E/SQLiteDatabase(25335): at com.example.databasebrowser.MyActivity.onClick(MyActivity.java:66)
04-01 22:24:56.470: E/SQLiteDatabase(25335): at android.view.View.performClick(View.java:3538)
04-01 22:24:56.470: E/SQLiteDatabase(25335): at android.view.View$PerformClick.run(View.java:14330)
04-01 22:24:56.470: E/SQLiteDatabase(25335): at android.os.Handler.handleCallback(Handler.java:608)
04-01 22:24:56.470: E/SQLiteDatabase(25335): at android.os.Handler.dispatchMessage(Handler.java:92)
04-01 22:24:56.470: E/SQLiteDatabase(25335): at android.os.Looper.loop(Looper.java:156)
04-01 22:24:56.470: E/SQLiteDatabase(25335): at android.app.ActivityThread.main(ActivityThread.java:4977)
04-01 22:24:56.470: E/SQLiteDatabase(25335): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 22:24:56.470: E/SQLiteDatabase(25335): at java.lang.reflect.Method.invoke(Method.java:511)
04-01 22:24:56.470: E/SQLiteDatabase(25335): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-01 22:24:56.470: E/SQLiteDatabase(25335): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-01 22:24:56.470: E/SQLiteDatabase(25335): at dalvik.system.NativeStart.main(Native Method)
04-01 22:24:56.480: E/AndroidRuntime(25335): FATAL EXCEPTION: main
04-01 22:24:56.480: E/AndroidRuntime(25335): android.database.sqlite.SQLiteException: no such table: android_metadata
04-01 22:24:56.480: E/AndroidRuntime(25335): at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)
04-01 22:24:56.480: E/AndroidRuntime(25335): at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:2186)
04-01 22:24:56.480: E/AndroidRuntime(25335): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1094)
04-01 22:24:56.480: E/AndroidRuntime(25335): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1050)
04-01 22:24:56.480: E/AndroidRuntime(25335): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1026)
04-01 22:24:56.480: E/AndroidRuntime(25335): at com.example.databasebrowser.DbH.opendatabase(DbH.java:98)
04-01 22:24:56.480: E/AndroidRuntime(25335): at com.example.databasebrowser.MyActivity.validateLogin(MyActivity.java:105)
04-01 22:24:56.480: E/AndroidRuntime(25335): at com.example.databasebrowser.MyActivity.onClick(MyActivity.java:66)
04-01 22:24:56.480: E/AndroidRuntime(25335): at android.view.View.performClick(View.java:3538)
04-01 22:24:56.480: E/AndroidRuntime(25335): at android.view.View$PerformClick.run(View.java:14330)
04-01 22:24:56.480: E/AndroidRuntime(25335): at android.os.Handler.handleCallback(Handler.java:608)
04-01 22:24:56.480: E/AndroidRuntime(25335): at android.os.Handler.dispatchMessage(Handler.java:92)
04-01 22:24:56.480: E/AndroidRuntime(25335): at android.os.Looper.loop(Looper.java:156)
04-01 22:24:56.480: E/AndroidRuntime(25335): at android.app.ActivityThread.main(ActivityThread.java:4977)
04-01 22:24:56.480: E/AndroidRuntime(25335): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 22:24:56.480: E/AndroidRuntime(25335): at java.lang.reflect.Method.invoke(Method.java:511)
04-01 22:24:56.480: E/AndroidRuntime(25335): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-01 22:24:56.480: E/AndroidRuntime(25335): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-01 22:24:56.480: E/AndroidRuntime(25335): at dalvik.system.NativeStart.main(Native Method)