我使用this着名的数据库帮助程序,并希望将数据库复制到设备。 我已经填充并通过adb将数据库推送到模拟器,它在模拟器上工作正常,但是应用程序很难将数据库复制到设备。
我还创建了一个assets
文件夹,并将数据库放在~/Shiny/app/src/main/assets/database/shiny.db
。
这是我的主要活动:
以下是我在DataBaseHelper
帮助程序类中的应用程序特定变量:
public class DataBaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "shiny.db";
private static String DB_PATH = "/data/data/com.shinyapp.shiny/databases/";
private SQLiteDatabase myDataBase;
private Context myContext;
这是调用DataBaseHelper的onCreate方法:
但是我收到了这个错误:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Get a support ActionBar corresponding to this toolbar
ActionBar ab = getSupportActionBar();
// Enable the Up button
ab.setDisplayHomeAsUpEnabled(true);
myDB = new DataBaseHelper(this);
try {
myDB.createDataBase();
} catch (IOException e) {
throw new Error("Unable to create database");
}
try {
myDB.openDataBase();
}catch(SQLException sqle){
throw new Error("Unable to open database");
}
但是我收到了这个错误:
FATAL EXCEPTION: main
java.lang.Error: Error copying database
at com.shinyapp.shiny.DataBaseHelper.createDataBase(DataBaseHelper.java:61)
at com.shinyapp.shiny.MainActivity.onCreate(MainActivity.java:52)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
at android.app.ActivityThread.access$600(ActivityThread.java:128)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4514)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
我想知道这里有什么问题,我该如何解决?
更新:这里是我修改错误处理以记录错误而不是仅仅抛出错误时得到的错误日志:
05-27 16:13:08.196 23913-23913/com.shinyshop.shiny E/SqliteDatabaseCpp: sqlite3_open_v2("/data/data/com.shinyshop.shiny/databases/shiny.db", &handle, 1, NULL) failed
05-27 16:13:08.204 23913-23913/com.shinyshop.shiny E/SQLiteDatabase: Failed to open the database. closing it.
android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
app at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
app at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1124)
app at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1075)
app at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1051)
app at com.shinyshop.shiny.DataBaseHelper.checkDataBase(DataBaseHelper.java:78)
app at com.shinyshop.shiny.DataBaseHelper.createDataBase(DataBaseHelper.java:45)
app at com.shinyshop.shiny.MainActivity.onCreate(MainActivity.java:49)
app at android.app.Activity.performCreate(Activity.java:4465)
app at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
app at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
app at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
app at android.app.ActivityThread.access$600(ActivityThread.java:128)
app at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
app at android.os.Handler.dispatchMessage(Handler.java:99)
app at android.os.Looper.loop(Looper.java:137)
app at android.app.ActivityThread.main(ActivityThread.java:4514)
app at java.lang.reflect.Method.invokeNative(Native Method)
app at java.lang.reflect.Method.invoke(Method.java:511)
app at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
app at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
app at dalvik.system.NativeStart.main(Native Method)
05-27 16:13:08.360 23913-23913/com.shinyshop.shiny E/Creating DB ERROR: exception
app java.io.FileNotFoundException: shiny.db
app at android.content.res.AssetManager.openAsset(Native Method)
app at android.content.res.AssetManager.open(AssetManager.java:315)
app at android.content.res.AssetManager.open(AssetManager.java:289)
app at com.shinyshop.shiny.DataBaseHelper.copyDataBase(DataBaseHelper.java:103)
app at com.shinyshop.shiny.DataBaseHelper.createDataBase(DataBaseHelper.java:57)
app at com.shinyshop.shiny.MainActivity.onCreate(MainActivity.java:49)
app at android.app.Activity.performCreate(Activity.java:4465)
app at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
app at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
app at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
app at android.app.ActivityThread.access$600(ActivityThread.java:128)
app at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
app at android.os.Handler.dispatchMessage(Handler.java:99)
app at android.os.Looper.loop(Looper.java:137)
app at android.app.ActivityThread.main(ActivityThread.java:4514)
app at java.lang.reflect.Method.invokeNative(Native Method)
app at java.lang.reflect.Method.invoke(Method.java:511)
app at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
app at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
app at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您使用的示例代码陈旧且糟糕。
话虽如此,您的问题是示例代码期望您的数据库位于~/Shiny/app/src/main/assets/shiny.db
,而不是~/Shiny/app/src/main/assets/database/shiny.db
。如果您想要资源~/Shiny/app/src/main/assets/database/shiny.db
,则需要修改AssetManager
代码,将database/
部分添加到您传递给open()
的值。