我是android开发的新手,这是我第一个使用数据库的应用程序。我在Ubuntu下使用DB Browser for SQlite创建了一个数据库。
似乎问题出现在marhmallow以下的Android设备上,但我并不完全确定。
这里是我复制数据库的代码:
//version number.
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "db.sqlite";
private static String DATABASE_PATH = "/data/data/com.andrea.risuscito_passaggi/databases/";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DBHelper(Context context ) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.myContext = context;
}
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
public void copyDataBase() throws IOException{
boolean bCopyOk = false;
try{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
// Path to the just created empty db
String outFileName = DATABASE_PATH + DATABASE_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>-1) myOutput.write(buffer, 0, length);
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
bCopyOk = true;
}catch(Exception ex){
ex.printStackTrace();
}finally{
if (bCopyOk) Log.v(TAG, "[copyDataBase] - Database copied OK!");
}
}
使用Android Nougat(7.0.x)的所有设备都可以正常工作但是,如果我在另一个版本的android上测试这个应用程序,我会发现此错误日志:
03-22 20:08:21.660 9010-9010/com.andrea.risuscito_passaggi E/SQLiteLog: (14) cannot open file at line 31278 of [2ef4f3a5b1]
03-22 20:08:21.661 9010-9010/com.andrea.risuscito_passaggi E/SQLiteLog: (14) os_unix.c:31278: (2) open(/data/data/com.andrea.risuscito_passaggi/databases/db.sqlite) -
03-22 20:08:21.661 9010-9010/com.andrea.risuscito_passaggi E/SQLiteDatabase: Failed to open database '/data/data/com.andrea.risuscito_passaggi/databases/db.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:207)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
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:845)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:830)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:723)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:690)
at com.andrea.risuscito_passaggi.DBHelper.openDataBase(DBHelper.java:154)
at com.andrea.risuscito_passaggi.MainActivity.onCreate(MainActivity.java:94)
at android.app.Activity.performCreate(Activity.java:6304)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2539)
at android.app.ActivityThread.access$900(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5507)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-22 20:08:21.662 9010-9010/com.andrea.risuscito_passaggi W/SQLiteDatabase: Retry to open database[/data/data/com.andrea.risuscito_passaggi/databases/db.sqlite] due to error: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
03-22 20:08:21.662 9010-9010/com.andrea.risuscito_passaggi E/SQLiteLog: (14) cannot open file at line 31278 of [2ef4f3a5b1]
03-22 20:08:21.662 9010-9010/com.andrea.risuscito_passaggi E/SQLiteLog: (14) os_unix.c:31278: (2) open(/data/data/com.andrea.risuscito_passaggi/databases/db.sqlite) -
03-22 20:08:21.663 9010-9010/com.andrea.risuscito_passaggi E/SQLiteDatabase: Failed to open database '/data/data/com.andrea.risuscito_passaggi/databases/db.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:207)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
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:845)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:830)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:732)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:690)
at com.andrea.risuscito_passaggi.DBHelper.openDataBase(DBHelper.java:154)
at com.andrea.risuscito_passaggi.MainActivity.onCreate(MainActivity.java:94)
at android.app.Activity.performCreate(Activity.java:6304)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2539)
at android.app.ActivityThread.access$900(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5507)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我搜索了很多,但我找不到任何解决方案。感谢
编辑: 我停止只在java代码中查找错误,我开始研究数据库代码。我发现了两个错误:
未创建空数据库的文件夹,因此我在复制数据库之前添加了此代码
// check if databases folder exists, if not create one and its subfolders
File databaseFile = new File( DATABASE_PATH);
if (!databaseFile.exists()){
databaseFile.mkdir();
}
数据库中有一个被破坏的视图,我无法解决它,但我通过编写java中的查询解决了
答案 0 :(得分:0)
我停止只在java代码中查找错误,我开始研究数据库代码。我发现了两个错误:
1.未创建空数据库的文件夹,因此我在复制数据库之前添加了此代码
// check if databases folder exists, if not create one and its subfolders
File databaseFile = new File( DATABASE_PATH);
if (!databaseFile.exists()){
databaseFile.mkdir();
}