0
現在我發佈了我的報價應用程序,它有一個用於收藏夾報價的部分。用戶可以將任何報價添加到收藏夾,但是如果我在數據庫中添加一些報價並更新了Google Play上的應用,則當用戶更新時,他們會丟失他們的收藏夾,就像刪除舊數據庫一樣如何在不丟失舊數據的情況下在sqlite數據庫上添加數據?
他們如何將其更新爲獲取新的報價而不失去他們的最愛?
public DAO(Context context) {
dbHandler = new DataBaseHandler(context);
try {
dbHandler.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
dbHandler.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
Log.e("path2", context.getDatabasePath("SuccessQuotes").toString());
// open();
}
// ==============================================================================
// Getting All Quotes
public Cursor getQuotes(String start) {
// Select All Query
String query = "SELECT * FROM " + TABLE_QUOTES + " JOIN " + TABLE_AUTHORS + " ON " + QU_AUTHOR + " = " + AU_ID
+ " LIMIT " + start + ",50";
Cursor cursor = database.rawQuery(query, null);
cursor.moveToFirst();
return cursor;
}
// ==============================================================================
// Getting All Quotes
public Cursor getFavoriteQuotes() {
// Select All Query
String query = "SELECT * FROM " + TABLE_QUOTES + " JOIN " + TABLE_AUTHORS + " ON " + QU_AUTHOR + " = " + AU_ID
+ " WHERE " + QU_FAVORITE + " = " + "1";
Cursor cursor = database.rawQuery(query, null);
cursor.moveToFirst();
return cursor;
}
// ==============================================================================
// Getting All Quotes
public Cursor getAuthorQuotes(String authorID) {
// Select All Query
String query = "SELECT * FROM " + TABLE_QUOTES + " JOIN " + TABLE_AUTHORS + " ON " + QU_AUTHOR + " = " + AU_ID
+ " WHERE " + QU_AUTHOR + " = '" + authorID + "'";
Cursor cursor = database.rawQuery(query, null);
cursor.moveToFirst();
return cursor;
}
// ==============================================================================
// Getting All Quotes
public Cursor getOneQuote(String quoteID) {
// Select All Query
String query = "SELECT * FROM " + TABLE_QUOTES + " JOIN " + TABLE_AUTHORS + " ON " + QU_AUTHOR + " = " + AU_ID
+ " WHERE " + QU_ID + " = '" + quoteID + "'";
Cursor cursor = database.rawQuery(query, null);
cursor.moveToFirst();
return cursor;
}
// ==============================================================================
public void addOrRemoveFavorites(String id, String value) {
ContentValues values = new ContentValues();
values.put(QU_FAVORITE, value);
// Update Row
database.update(TABLE_QUOTES, values, QU_ID + "=?", new String[] { id });
}
// ==============================================================================
// Getting All Quotes
public Cursor getAllAuthors() {
// Select All Query
String query = "SELECT *, COUNT(" + QU_AUTHOR + ") AS count FROM " + TABLE_AUTHORS + " JOIN " + TABLE_QUOTES
+ " ON " + AU_ID + " = " + QU_AUTHOR + " GROUP BY " + AU_NAME + " ORDER BY " + AU_NAME + " ASC";
Cursor cursor = database.rawQuery(query, null);
cursor.moveToFirst();
return cursor;
}
// ==============================================================================
public void updateSetting(String field, String value) {
open();
ContentValues values = new ContentValues();
values.put(field, value);
// Update Row
database.update(TABLE_SETTINGS, values, null, null);
}
// ==============================================================================
public Cursor getSettings() {
open();
String query = "SELECT * FROM " + TABLE_SETTINGS;
Cursor cursor = database.rawQuery(query, null);
cursor.moveToFirst();
return cursor;
}
// ==============================================================================
public void open() throws SQLException {
database = dbHandler.getReadableDatabase();
}
// ==============================================================================
public void closeDatabase() {
dbHandler.close();
}
您是否更改數據庫的版本? – Sree
怎麼改變它? –
如果你點改變DATABASE_VERSION沒有什麼可擔心的,否則你改變了DATABASE_VERSION有機會損失數據, – Sree