[android-developers] Crash on SQLiteOpenHelper::getReadableDatabase when I'm trying to copy new db from raw resources
Hi Guys,
I ship a pre-created database as a raw resource and copy it into my
app's
database folder at startup. When I ship updates I overwrite the old db
with
my new db, which sometimes includes new stuff.
I can't reproduce this with any of my devices, but some of my app's
users
get a crash when my app calls this.getReadableDatabase() in my
override for
createDataBase:
android.database.sqlite.SQLiteException:
Can't upgrade read-only database from version 62 to 1: /data/data/
com.myapp.myapp/databases/ls.db
at
android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:
236)
at com.myapp.myapp.DataBaseHelper.createDataBase(DataBaseHelper.java:
75)
at com.myapp.myapp.myapp.MyApp$11.run(MyApp.java:675)
at java.lang.Thread.run(Thread.java:1020)
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exists
}else{
// delete the old database
this.getReadableDatabase(); <<<---- Exception is thrown HERE
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
I copy the database as follows:
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = null;
// 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);
myInput = myContext.getResources().openRawResource(R.raw.ls_db);
//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);
}
myInput.close();
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
openDataBase();
myDataBase.setVersion(packageVers);
// Copy stored data to new database
...
close();
}
////////////////////////
Is the call to:
// delete the old database
this.getReadableDatabase();
problematic? What I really want to do is
chmod 777 myolddb
rm -f myolddb
if this were plain old Unix. What situations can lead to Android
treating this
as a read-only entity that can't be overwritten? This database is
originally created
by my app, and I only overwrite it on app updates.
Thanks!!
pawpaw17
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home