I use this tutorial: Tutorial Custom database
I copy database from assets folder and copy it to my device(or emulator). Everything is correct. I see my database in DDMS perspective. But I want aslo upgrade my database sometimes so i did:
super(context, DB_NAME, null, 2); //changed version from 1 to 2
and modify onUpgrade method:
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(newVersion > oldVersion){
this.myContext.deleteDatabase(DB_NAME);
try {
this.copyDataBase();
} catch (IOException e) {
e.printStackTrace();
}
}
}
But after run I have still old version of database on my device. How I can delete old version of database and copy new. DB_NAME is my database name(with format, but not path) and copyDataBase() is method that copy database to device(and it's work).
I paste my all code:
public class DataBaseHelper extends SQLiteOpenHelper{
private static String DB_PATH = "/data/data/sitcom.quiz/databases/";
private static String DB_NAME = "sitcoms.sqlite";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 4);
this.myContext = context;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// 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();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d("adas", "dasd");
if(newVersion > oldVersion){
String myPath = DB_PATH + DB_NAME;
this.myContext.deleteDatabase(myPath);
try {
this.copyDataBase();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
and my activity:
DataBaseHelper myDbHelper = new DataBaseHelper(this);
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
Thanks if you can give me reason or only hint why this don' t work.
When using your own SQLite database in Android, most people take for granted future releases of the application they are working on. Looking at the Android Developer documentation can also lead you down a rickety path. Upgrading databases in Android is difficult. I’ve been there and battled the fires in production. It is not pretty.
If you want more, simply add more "insert" statements in a method such as populateDatabase (). "How to upgrade sqlite database..." Upgrade implies you want to change the database schema (e.g., add more columns).
Now in order to make an upgrade to your database you can do the following: Create a SQL file and name it from_2_to_3.sql according to the version of the database you are upgrading from and to. In that file you can create all your SQL statements.
The better solution that we found (somewhere on the internet – can’t find the link now) was to store versioned sql files in the assets/ folder in the application. This ensures that each step of the process of the upgrade is correct.
android already opened the db when the function onUpgrade() is called. i don't think it is possible to delete it at this time. check the log for more info. if you wanna do it this way, you need to move the delete and copy code to a place where the db is not open yet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With