Sunday, June 3, 2012

Re: [android-developers] Re: Multiple sqlite db upgrade

Actually I need to add some new data to all the databases and want to keep the previous data inserted by the user. Now the problem is in the onUpgrade() method i get only one db as a input parameter so how can i get the previous data of  user of other db.


On Sun, Jun 3, 2012 at 12:04 AM, lbendlin <lutz@bendlin.us> wrote:
what exactly do you need to upgrade? Can't you just run a few scripts in onUpgrade  ?


On Saturday, June 2, 2012 8:02:22 AM UTC-4, RawCoder wrote:
Hi guys,

I am working on an android application where i have to use multiple databases. I have used previously created db file. Actually i am copying the db file from asset folder.
Now there is option to add data for the user(add favorites). The user data will be added in all three db. 

Now the problem is i need to upgrade all the db when there is a new version of the app. I have to keep the user data inserted by the user and add the changes of the new db.
I have done some googling and find solution to upgrade one db but i can not find a solution to upgrade all the databases and keep the old versions user data.

Here is my dbhelper class:

//////////////////////////////////////////////////////////////////////////////////////////////////
package riskycoder.database.helper;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;

import riskycoder.utils.AppStaticValues;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataBaseHelper extends SQLiteOpenHelper {
private static String DB_PATH = "/data/data/riskycoder.login/databases/";
public static String DB_NAME = "datenbank_EN.sqlite";
private SQLiteDatabase myDataBase;
private final Context myContext;
private static final int DATABASE_VERSION = 1;
private static final String DB_NAME_ENG = "datenbank_EN.sqlite";
private static final String DB_NAME_GER = "datenbank_DE.sqlite";
private static final String DB_NAME_SPA = "datenbank_SP.sqlite";
private Cursor updateCursor;

public static void setDataBaseName(String lan)
{
if(lan.equals("ENG"))
{
DB_NAME=DB_NAME_ENG;
}
else if(lan.equals("GER"))
{
DB_NAME=DB_NAME_GER;
}
else if(lan.equals("SPA"))
{
DB_NAME=DB_NAME_SPA;
}
}
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
this.myContext = context;
updateCursor=null;
}

public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
this.getWritableDatabase();
} else {
this.getWritableDatabase();
try {
this.close();
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_READWRITE);
} catch (SQLiteException e) {
}
if (checkDB != null)
checkDB.close();
return checkDB != null ? true : false;
}

private void copyDataBase() throws IOException {

InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[2048];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
//myDataBase.setVersion(DATABASE_VERSION);
}

public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
Log.d("Test", "Database version: " +myDataBase.getVersion());
if(updateCursor!=null && updateCursor.getCount()!=0)
{
updateCursor.moveToFirst();
do{
myDataBase.execSQL("UPDATE Content SET is_favourite='1' WHERE  content_id ='"+updateCursor.getInt(updateCursor.getColumnIndex("content_id"))+"' AND  category_id = '"+updateCursor.getInt(updateCursor.getColumnIndex("category_id"))+"'");
}while(updateCursor.moveToNext());
updateCursor.close();
}
}

@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
public Cursor Get_Header_Cursor(){
Cursor cursor;
cursor = myDataBase.rawQuery("select * from Headers ORDER BY header_orderId",null);
return cursor;
}
public Cursor Get_Group_Cursor(int id){
Cursor cursor = myDataBase.rawQuery("select * from Categories where header_id = "+id+" ORDER BY category_orderId",null);
return cursor;
}
public Cursor Get_Content_Cursor(int id){
Cursor cursor = myDataBase.rawQuery("select * from Content where category_id = "+id,null);
return cursor;
}

public Cursor Get_Favourite_Cursor(){
Cursor cursor = myDataBase.rawQuery("select * from Content where is_favourite = 1",null);
return cursor;
}

@Override
public void onCreate(SQLiteDatabase db) {
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(newVersion>oldVersion)
{
updateCursor = db.rawQuery("select * from Content where is_favourite = 1",null);
Log.d("Test", "is fav"+updateCursor.getCount());
try {
copyDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////

Please help how to upgrade all the databases.......

--
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

--
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


Real Estate