I'm new to SQLite and this is the first database that I'm using in an App, so I'm having some troubles.
At first, I could use the database with no problems, I was fetching the data correctly and it was working fine. But I realized that, after reinstalling the App, it was throwing me a SQLite cannot open file error.
Now it's pretty much crashed and I don't know why. It never works, it always throws me the Cannot open file error.
I never write data to my database, I only use it to fetch some data. This is the content of my DatabaseHelper class:
public static String DB_PATH = "/data/data/myapp.app.databasetest/databases/";
public static String DB_NAME = "mydatabase.db"; public static final int DB_VERSION = 2;
private SQLiteDatabase myDB;
private Context context;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.context = contex
}
//check if database exists
private boolean checkDataBase() {
SQLiteDatabase tempDB = null;
try {
String myPath = DB_PATH + DB_NAME;
//the error appears in the next line
tempDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); //<--Here appears the ERROR
} catch (SQLiteException e) {
Log.e("tle99 - check", e.getMessage());
}
if (tempDB != null)
tempDB.close();
return tempDB != null ? true : false;
}
//copy database to assets folder
public void copyDataBase() throws IOException {
try {
InputStream myInput = context.getAssets().open(DB_NAME);
String outputFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outputFileName);
byte[] buffer = new byte[1024];
int length;
while((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
} catch (Exception e) {
Log.e("tle99 - copyDatabase", e.getMessage());
}
}
public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
myDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
Log.e("tle99 - create", e.getMessage());
}
}
}
The error appears when calling the checkDataBase() function (there's a comment next to the line) and as I said it's a SQLite cannot open file. This is the part in the Logcat where I see the error:
03-31 19:09:18.464 21825-21825/? E/SQLiteLog: (14) cannot open file at line 30052 of [b3bb660af9]
03-31 19:09:18.464 21825-21825/? E/SQLiteLog: (14) os_unix.c:30052: (2) open(/data/data/myapp.app.databasetest/databases/mydatabase.db) -
03-31 19:09:18.466 21825-21825/? E/SQLiteDatabase: Failed to open database '/data/data/myapp.app.databasetest/databases/mydatabase.db'.
I sincerely don't know how to fix it. I've been stuck on it for a couple days now and it's being a real pain to fix. It was working fine until I would reinstall the App, but now it's never working. I don't know what's wrong with my code.
Any help would be much appreciated! Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire