I am struggling with my database which used to work before I uninstalled my app (ironically that's what I had to do the first time to get it working) anyway I've redone my code because I want to avoid using hard code path such as private static final String DB_PATH = "/data/data/my_package/databases/";
When I use the code below, I get the full database path from the System.out.println which is "/data/data/my_package/databases/lm.sqlite";
File outFile =context.getDatabasePath(DB_NAME);
String fullPATH =outFile.getPath();
System.out.println("PRINT OUTPUT FILENAME: " + fullPATH)
DatabaseHelper:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "lm.sqlite";
private static final int DB_VERSION = 1;
private static final String TABLE_LM = "lmtable";
//private static final String DB_PATH = "/data/data/my_package/databases/";
private SQLiteDatabase myDB;
private Context context;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.context = context;
File outFile =context.getDatabasePath(DB_NAME);
String fullPATH =outFile.getPath();
System.out.println("PRINT OUTPUT FILENAME: " + fullPATH);
}
How should I change my previous DB_PATH in checkDataBase(), copyDataBase() and openDataBase() methods, I tried to change FROM String myPath = DB_PATH + DB_NAME TO String myPath = fullPATH but an error is returned: fullPATH cannot be resolved to a variable
DatabaseHelper Methods: (Note DB_PATH)
private boolean checkDataBase() {
SQLiteDatabase tempDB = null;
try {
String myPath = DB_PATH + DB_NAME;
tempDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
Log.e("ts01 - check", e.getMessage());
}
if (tempDB != null)
tempDB.close();
return tempDB != null ? true : false;
}
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("ts02 - copyDatabase", e.getMessage());
}
}
public void openDataBase() throws SQLException{
String myPath = DB_PATH + DB_NAME;
myDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
Aucun commentaire:
Enregistrer un commentaire