I have one quote application which have sqllite database. I have published first version of application which have not any field for database version. Now I need to republish application with database version=1. I have working code for quotes which have database version 5. I am trying my self for use this code but getting issue of copy data. If I want update application which have database version 1, which condition should I apply so old user who have installed application without database version field and new user also not face any issue ? My database handler class is like below. Thanks
public class DataBaseHandler extends SQLiteOpenHelper {
private static String DB_PATH;
private static String DB_NAME = "SuccessQuotesNew";
private SQLiteDatabase myDataBase;
private static int DATABASE_VERSION = 5;
private final Context myContext;
public DataBaseHandler(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
this.myContext = context;
DB_PATH = context.getDatabasePath(DB_NAME).toString();
Log.e("path", DB_PATH);
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if(dbExist)
{
if(DATABASE_VERSION == 1)
{
try {
copyDataBase();
DATABASE_VERSION = 4;
} catch (IOException e) {
throw new Error("Error copying database");
}
}
else if (DATABASE_VERSION == 2)
{
try {
copyDataBase();
DATABASE_VERSION = 4;
} catch (IOException e) {
throw new Error("Error copying database");
}
}
else if (DATABASE_VERSION == 3)
{
try {
copyDataBase();
DATABASE_VERSION = 4;
} catch (IOException e) {
throw new Error("Error copying database");
}
}
else if (DATABASE_VERSION == 4)
{
try {
copyDataBase();
DATABASE_VERSION = 5;
} catch (IOException e) {
throw new Error("Error copying database");
}
}else
{
SQLiteDatabase database = null;
database = this.getWritableDatabase();
String query_count = "SELECT version FROM users";
Cursor c_count = database.rawQuery(query_count, null);
c_count.moveToFirst();
Integer count = c_count.getInt(c_count.getColumnIndex("version"));
if(count == DATABASE_VERSION)
{
}else
{
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
}else
{
this.getReadableDatabase();
try {
copyDataBase();
DATABASE_VERSION = 2;
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH;
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;
// 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;
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) {
}
}
Aucun commentaire:
Enregistrer un commentaire