Can SQLite file copied successfully on the data folder of an unrooted android device ?
Posted
by
student
on Stack Overflow
See other posts from Stack Overflow
or by student
Published on 2012-10-09T02:24:04Z
Indexed on
2012/10/09
3:37 UTC
Read the original article
Hit count: 127
I know that in order to access the data folder on the device, it needs to be rooted. However, if I just want to copy the database from my assets folder to the data folder on my device, will the copying process works on an unrooted phone? The following is my Database Helper class. From logcat, I can verify that the methods call to copyDataBase(), createDataBase() and openDataBase() are returned successfully. However, I got this error message
android.database.sqlite.SQLiteException: no such table: TABLE_NAME:
when my application is executing rawQuery. I'm suspecting the database file is not copied successfully (cannot be too sure as I do not have access to data folder), yet the method call to copyDatabase() are not throwing any exception. What could it be? Thanks.
ps: My device is still unrooted, I hope it is not the main cause of the error.
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
String s = new Boolean(dbExist).toString();
Log.d("dbExist", s );
if(dbExist){
//do nothing - database already exist
Log.d("createdatabase","DB exists so do nothing");
}else{
this.getReadableDatabase();
try {
copyDataBase();
Log.d("copydatabase","Successful return frm method call!");
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase(){
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = null;
myInput = myContext.getAssets().open(DB_NAME);
Log.d("copydatabase","InputStream successful!");
// Path to the just created empty db
String outFileName = DB_PATH + DB_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))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
/* @Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}*/
public void close() {
// NOTE: openHelper must now be a member of CallDataHelper;
// you currently have it as a local in your constructor
if (myDataBase != null) {
myDataBase.close();
}
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
© Stack Overflow or respective owner