我有一个从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)
答案 0 :(得分:0)
您应该继承SQLiteOpenHelper以避免数据库创建错误。这可以处理所有数据库创建,更新和检查存在问题 - 无需自己完成。此外,它将在默认的推荐路径中创建DB文件。
以下内容可以是您可以在代码中使用的DB open helper类。
class myDbHelper extends SQLiteOpenHelper {
public myDbHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
// Called when no database exists on disk
@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DATABASE_CREATE);
}
// Called when there is a database version mismatch meaning that
// the version of the database on disk needs to be upgraded to
// the current version.
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion,
int _newVersion) {
// Upgrade the existing database to conform to the new version.
_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
// Create a new one.
onCreate(_db);
}
}
Lars Vogel对此有一个nice tutorial。