Reputation: 61
I'm new in android development and this is my first app with a database. I created a database with DB Browser for SQlite under Ubuntu.
It seems that the problem is present on Android devices below marhmallow but i'm not completely sure.
here ther's my code to copy database:
//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!");
}
}
With all devices with Android Nougat (7.0.x) all works fine but, if i test this app on another version of android, i find this error log:
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)
I've searched a lot but i don't find any solution. Thanks
EDIT: I stopped looking for the error only in java code and I began to look into the database code. I found two errors:
the folder to the empty database was not created, so I added this code before copy database
// check if databases folder exists, if not create one and its subfolders
File databaseFile = new File( DATABASE_PATH);
if (!databaseFile.exists()){
databaseFile.mkdir();
}
there was a view in the database that was clobbered, I could not fix it but I solved by writing the query from java
Upvotes: 2
Views: 1004
Reputation: 61
I stopped looking for the error only in java code and I began to look into the database code. I found two errors:
1.the folder to the empty database was not created, so I added this code before copy database
// check if databases folder exists, if not create one and its subfolders
File databaseFile = new File( DATABASE_PATH);
if (!databaseFile.exists()){
databaseFile.mkdir();
}
Upvotes: 1