Reputation: 127
How to upgrade read only sqlite database
I have tried to change to SQLiteDatabase.OPEN_READWRITE);
and getReadableDatabase();
the result still the same.
When I want to upgrade my sqlite datatbase from version 1 to 2 , I got this error :
SQLiteException: Can't upgrade read-only database from version X to Y
In the 1st verison of my apps , I use this code :
private static final String DB_NAME = "this_the_database";
private final Context context;
private static final int DATABASE_VERSION = 1;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
this.context = context;
String path = context.getDatabasePath(DB_NAME).getAbsolutePath();
SQLiteDatabase db = null;
try {
db = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) { // DB not found
db = getReadableDatabase();
db.close();
// Copy DB from asset folder.
copyDatabase(path);
}
if (db != null) {
db.close();
}
}
when I decided to upgarde the database using this , it crashed
@Override
public void onCreate(SQLiteDatabase db) {
new DatabaseHelper(context).getReadableDatabase();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DB_NAME);
onCreate(db);
}
Ref: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
Upvotes: 2
Views: 2987
Reputation: 4473
you cant get a readable database on create but even if you could, the database is already passed in (SQLiteDatabase db) and there is no content to read at that moment. on create is only called when the database is first created on application start and you can only add/remove columns on create or on upgrade. now what are you trying to accomplish with this?
Upvotes: 0
Reputation: 5575
Why do you use SQLiteDatabase.openDatabase? It should be sufficient to have just the call to super(..) in the DatabaseHelper constructor. The getReadableDatabase() can be in an open() method or can be called by the activity opening the database. In the apidemos, there is a LoaderThrottle example that shows how to use the Android SQLite database.
Upvotes: 1