我正在使用公开的代码将我预先填充的数据库复制到android。问题是我在复制数据库时遇到错误。它将创建文件,但数据库只包含" android-metadata"表
这是我的ExternalDbOpenHelper类
package com.solosus.mhd;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class ExternalDbOpenHelper extends SQLiteOpenHelper {
//Path to the device folder with databases
public static String DB_PATH;
//Database file name
public static String DB_NAME;
public SQLiteDatabase database;
public final Context context;
public SQLiteDatabase getDb() {
return database;
}
public ExternalDbOpenHelper(Context context, String databaseName) {
super(context, databaseName, null, 1);
this.context = context;
//Write a full path to the databases of your application
String packageName = context.getPackageName();
DB_PATH = String.format("//data//data//%s//databases//", packageName);
DB_NAME = databaseName;
openDataBase();
}
//This piece of code will create a database if it’s not yet created
public void createDataBase() {
boolean dbExist = checkDataBase();
if (!dbExist) {
// this.getReadableDatabase();
SQLiteDatabase db = this.getReadableDatabase();
if (db.isOpen()){
db.close();
}
try {
copyDataBase();
} catch (IOException e) {
Log.e(this.getClass().toString(), "Copying error");
throw new Error("Error copying database!");
}
} else {
Log.i(this.getClass().toString(), "Database already exists");
}
}
//Performing a database existence check
public boolean checkDataBase() {
SQLiteDatabase checkDb = null;
try {
String path = DB_PATH + DB_NAME;
checkDb = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLException e) {
Log.e(this.getClass().toString(), "Error while checking db");
Log.d("CHYBA", e.toString());
}
//Android doesn’t like resource leaks, everything should
// be closed
if (checkDb != null) {
checkDb.close();
}
return checkDb != null;
}
//Method for copying the database
private void copyDataBase() throws IOException {
//Open a stream for reading from our ready-made database
//The stream source is located in the assets
// if (database.isOpen())
// database.close();
InputStream externalDbStream = context.getAssets().open(DB_NAME);
//Path to the created empty database on your Android device
String outFileName = DB_PATH + DB_NAME;
//Now create a stream for writing the database byte by byte
OutputStream localDbStream = new FileOutputStream(outFileName);
//Copying the database
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = externalDbStream.read(buffer)) > 0) {
localDbStream.write(buffer, 0, bytesRead);
}
//Don’t forget to close the streams
localDbStream.close();
externalDbStream.close();
}
public SQLiteDatabase openDataBase() throws SQLException {
String path = DB_PATH + DB_NAME;
if (database == null) {
createDataBase();
database = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READWRITE);
}
return database;
}
@Override
public synchronized void close() {
if (database != null) {
database.close();
}
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
// public Cursor select(String query) {
// Cursor c = database.rawQuery(query, null);
// return c;
//
// }
}
这是我得到的错误(在模拟器上首次运行应用程序 - 擦除用户数据,模拟器上不存在数据库)
10-16 09:04:34.8701106-1106/com.solosus.mhd D/dalvikvm﹕ GC_FOR_ALLOC freed 89K, 6% free 2952K/3128K, paused 127ms, total 128ms
10-16 09:04:34.8701106-1106/com.solosus.mhd I/dalvikvm-heap﹕ Grow heap (frag case) to 3.570MB for 635812-byte allocation
10-16 09:04:34.9301106-1115/com.solosus.mhd D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 5% free 3573K/3752K, paused 43ms, total 43ms
10-16 09:04:35.0901106-1106/com.solosus.mhd E/SQLiteLog﹕ (14) cannot open file at line 30191 of [00bb9c9ce4]
10-16 09:04:35.0901106-1106/com.solosus.mhd E/SQLiteLog﹕ (14) os_unix.c:30191: (2) open(//data//data//com.solosus.mhd//databases//Mhd_nove3.sqlite) -
10-16 09:04:35.1001106-1106/com.solosus.mhd E/SQLiteDatabase﹕ Failed to open database '//data//data//com.solosus.mhd//databases//Mhd_nove3.sqlite'.android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
at com.solosus.mhd.ExternalDbOpenHelper.checkDataBase(ExternalDbOpenHelper.java:67)
at com.solosus.mhd.ExternalDbOpenHelper.createDataBase(ExternalDbOpenHelper.java:44)
at com.solosus.mhd.ExternalDbOpenHelper.openDataBase(ExternalDbOpenHelper.java:110)
at com.solosus.mhd.ExternalDbOpenHelper.<init>(ExternalDbOpenHelper.java:39)
at com.solosus.mhd.MainActivity.onCreate(MainActivity.java:77)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
10-16 09:04:35.1001106-1106/com.solosus.mhd E/class com.solosus.mhd.ExternalDbOpenHelper﹕ Error while checking db
10-16 09:04:35.1001106-1106/com.solosus.mhd D/CHYBA﹕ android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
10-16 09:04:35.1901106-1106/com.solosus.mhd E/class com.solosus.mhd.ExternalDbOpenHelper﹕ Copying error
10-16 09:04:35.1901106-1106/com.solosus.mhd D/AndroidRuntime﹕ Shutting down VM
10-16 09:04:35.2001106-1106/com.solosus.mhd W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb3aa5ba8)
10-16 09:04:35.2001106-1106/com.solosus.mhd E/AndroidRuntime﹕
FATAL EXCEPTION: main Process: com.solosus.mhd, PID: 1106 java.lang.Error: Error copying database!
at com.solosus.mhd.ExternalDbOpenHelper.createDataBase(ExternalDbOpenHelper.java:55)
at com.solosus.mhd.ExternalDbOpenHelper.openDataBase(ExternalDbOpenHelper.java:110)
at com.solosus.mhd.ExternalDbOpenHelper.<init>(ExternalDbOpenHelper.java:39)
at com.solosus.mhd.MainActivity.onCreate(MainActivity.java:77)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
在此之后我的应用程序崩溃了。当我再次运行应用程序时,我的应用程序将启动,但我仍然会遇到一些错误
10-16 09:04:34.870 1106-1106/com.solosus.mhd D/dalvikvm﹕ GC_FOR_ALLOC freed 89K, 6% free 2952K/3128K, paused 127ms, total 128ms
10-16 09:04:34.870 1106-1106/com.solosus.mhd I/dalvikvm-heap﹕ Grow heap (frag case) to 3.570MB for 635812-byte allocation
10-16 09:04:34.930 1106-1115/com.solosus.mhd D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 5% free 3573K/3752K, paused 43ms, total 43ms
10-16 09:04:35.090 1106-1106/com.solosus.mhd E/SQLiteLog﹕ (14) cannot open file at line 30191 of [00bb9c9ce4]
10-16 09:04:35.090 1106-1106/com.solosus.mhd E/SQLiteLog﹕ (14) os_unix.c:30191: (2) open(//data//data//com.solosus.mhd//databases//Mhd_nove3.sqlite) -
10-16 09:04:35.100 1106-1106/com.solosus.mhd E/SQLiteDatabase﹕ Failed to open database '//data//data//com.solosus.mhd//databases//Mhd_nove3.sqlite'.android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
at com.solosus.mhd.ExternalDbOpenHelper.checkDataBase(ExternalDbOpenHelper.java:67)
at com.solosus.mhd.ExternalDbOpenHelper.createDataBase(ExternalDbOpenHelper.java:44)
at com.solosus.mhd.ExternalDbOpenHelper.openDataBase(ExternalDbOpenHelper.java:110)
at com.solosus.mhd.ExternalDbOpenHelper.<init>(ExternalDbOpenHelper.java:39)
at com.solosus.mhd.MainActivity.onCreate(MainActivity.java:77)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
10-16 09:04:35.100 1106-1106/com.solosus.mhd E/class com.solosus.mhd.ExternalDbOpenHelper﹕ Error while checking db
10-16 09:04:35.100 1106-1106/com.solosus.mhd D/CHYBA﹕ android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
10-16 09:04:35.190 1106-1106/com.solosus.mhd E/class com.solosus.mhd.ExternalDbOpenHelper﹕ Copying error
10-16 09:04:35.190 1106-1106/com.solosus.mhd D/AndroidRuntime﹕ Shutting down VM
10-16 09:04:35.200 1106-1106/com.solosus.mhd W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb3aa5ba8)
10-16 09:04:35.200 1106-1106/com.solosus.mhd E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.solosus.mhd, PID: 1106
java.lang.Error: Error copying database!
at com.solosus.mhd.ExternalDbOpenHelper.createDataBase(ExternalDbOpenHelper.java:55)
at com.solosus.mhd.ExternalDbOpenHelper.openDataBase(ExternalDbOpenHelper.java:110)
at com.solosus.mhd.ExternalDbOpenHelper.<init>(ExternalDbOpenHelper.java:39)
at com.solosus.mhd.MainActivity.onCreate(MainActivity.java:77)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
10-16 09:17:17.542 1272-1272/com.solosus.mhd D/dalvikvm﹕ GC_FOR_ALLOC freed 70K, 5% free 2952K/3108K, paused 77ms, total 78ms
10-16 09:17:17.542 1272-1272/com.solosus.mhd I/dalvikvm-heap﹕ Grow heap (frag case) to 3.570MB for 635812-byte allocation
10-16 09:17:17.572 1272-1281/com.solosus.mhd D/dalvikvm﹕ GC_FOR_ALLOC freed 2K, 5% free 3570K/3732K, paused 26ms, total 26ms
10-16 09:17:17.682 1272-1272/com.solosus.mhd I/class com.solosus.mhd.ExternalDbOpenHelper﹕ Database already exists
10-16 09:17:17.712 1272-1272/com.solosus.mhd I/class com.solosus.mhd.ExternalDbOpenHelper﹕ Database already exists
10-16 09:17:17.802 1272-1272/com.solosus.mhd I/class com.solosus.mhd.ExternalDbOpenHelper﹕ Database already exists
10-16 09:17:17.902 1272-1272/com.solosus.mhd D/﹕ HostConnection::get() New Host Connection established 0xb84be4b0, tid 1272
10-16 09:17:17.972 1272-1272/com.solosus.mhd W/EGL_emulation﹕ eglSurfaceAttrib not implemented
10-16 09:17:17.982 1272-1272/com.solosus.mhd D/OpenGLRenderer﹕ Enabling debug mode 0
现在我可以在DDMS中看到已在设备上创建了数据库文件,但它不包含任何表,除了&#34; android-metadata&#34;。 我调试了应用程序,问题是当调用方法copyDatabase()时它只执行第一行然后跳转到调用方法的catch语句
private void copyDataBase() throws IOException {
//Open a stream for reading from our ready-made database
//The stream source is located in the assets
// if (database.isOpen())
// database.close(); i saw some tip to add this but it did not work
InputStream externalDbStream = context.getAssets().open(DB_NAME); // after this line it jumps to catch-statement, the rest of the method is not executed.
这是调用copyDataBase()
的方法public void createDataBase() {
boolean dbExist = checkDataBase();
if (!dbExist) {
// this.getReadableDatabase();
SQLiteDatabase db = this.getReadableDatabase();
if (db.isOpen()){
db.close();
}
try {
copyDataBase();
} catch (IOException e) {
Log.e(this.getClass().toString(), "Copying error"); // after copyDataBase() first line it jumps right here
throw new Error("Error copying database!");
}
} else {
Log.i(this.getClass().toString(), "Database already exists");
}
}
我有点卡住因为我不知道我做错了什么。我不是专业人士,但我想问一下这里有什么问题。我调试了数百万次,无法找到究竟是什么错误。谢谢你的任何提示。 编辑:忘记提到我的数据库当前名为Mhd_nove3.sqlite,位于res目录中。