Reputation: 84
I have a quiz game and I keep in database high score and the questions in different tables, I use the following db adapter. If I update the database with more questions, it will overwrite the database, so it will do the same with high score. How I can keep the table with the high score so the player dont lose it, and update only questions table?
public class AnyDBAdapter {
private static final String TAG = "AnyDBAdapter";
private DatabaseHelper mDbHelper;
private static SQLiteDatabase mDb;
private static String DB_PATH = "/data/data/xx.xxxxx.xxxx/databases/";
private static final String DATABASE_NAME = "dbname";
private static final int DATABASE_VERSION = 1;
private final Context adapterContext;
public AnyDBAdapter(Context context) {
this.adapterContext = context;
}
public AnyDBAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(adapterContext);
try {
mDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
mDbHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
return this;
}
public void close() {
mDbHelper.close();
}
private static class DatabaseHelper extends SQLiteOpenHelper {
Context helperContext;
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
helperContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database!!!!!");
//db.execSQL("");
onCreate(db);
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
SQLiteDatabase db_Read = null;
if (dbExist) {
} else {
db_Read = this.getReadableDatabase();
db_Read.close();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
public SQLiteDatabase getDatabase() {
String myPath = DB_PATH + DATABASE_NAME;
return SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DATABASE_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
}
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 = helperContext.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();
}
public void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH + DATABASE_NAME;
mDb = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if (mDb != null)
mDb.close();
super.close();
}
}}
Upvotes: 2
Views: 211
Reputation: 317
Your DatabaseHelper needs to implement onUpgrade instead of just calling onCreate. Your code :
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database!!!!!");
//db.execSQL("");
onCreate(db);
}
Instead, write some SQL to modify the table you want in the db.execSQL line that you have commented out. The OS will pass you the oldVersion and newVersion so that you can make your modifications. Keep in mind, this is only for schema changes. If your database is staying the same structure, but you're just adding data, don't do anything except add fresh data to the table.
Upvotes: 1