Thursday, 12 September 2013

Android Difficulty in upgrading SQlite table without loosing old data

Android Difficulty in upgrading SQlite table without loosing old data

I my code for creating data base is this...
public class DatabaseHandler extends SQLiteOpenHelper {
// ====================== String Variables ======================
// The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.example/databases/";
private static final String DATABASE_NAME = "test.sqlite"; // Database Name
// ++++++++++++++++++++++ int Variables ++++++++++++++++++++++
private static final int DATABASE_VERSION = 1; // Database Version
// = = = = = = = = = = = Other Variables = = = = = = = = = = ==
private SQLiteDatabase myDataBase;
private final Context myContext;
SQLiteDatabase db;
@SuppressWarnings("unused")
private String privateDirectoryPath;
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.myContext = context;
// db = this.getWritableDatabase();
//this.privateDirectoryPath = privateDirectory;
}
public void db_open()
{
db = this.getWritableDatabase();
}
/**
* Creates a empty database on the system and rewrites it with your own
* database.
* */
public void createDataBase() throws IOException {
boolean dbExist = databaseExist();
if (dbExist) {
// do nothing - database already exist
} else {
// By calling this method and empty database will be created into
// the default system path
// of your application so we are gone a be able to overwrite that
// database with our database.
this.getReadableDatabase();
try {
copyDataBase(); // call method to copy database
db = this.getWritableDatabase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* Mrthod to check database is exist or not
* @return
*/
public boolean databaseExist()
{
File dbFile = new File(DB_PATH + DATABASE_NAME);
return dbFile.exists();
}
/**
* Copies your database from your local assets-folder to the just created
* empty database in the system folder, from where it can be accessed and
* handled. This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DATABASE_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();
}
/**
* Close Database
*/
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
public void DB_close()
{
if(db!=null)
db.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Now when i change new structure of data base from assets ... how it will
update in app.. without loosing old data.....
Please gave me proper giudence... Thanks in advance....

No comments:

Post a Comment