2014-04-07 73 views
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(); 
    } 
+1

您是否更改數據庫的版本? – Sree

+0

怎麼改變它? –

+1

如果你點改變DATABASE_VERSION沒有什麼可擔心的,否則你改變了DATABASE_VERSION有機會損失數據, – Sree

回答

1

當用戶更新從遊戲商店中的應用,你應該要遵循以下步驟: - 你最喜歡的

1-取備份從分貝臨時數組列表。
2-刪除舊數據庫。
3-創建新數據庫並在新數據庫中插入記錄。

注意: - 如果程序很大,那麼使用IntentService和靜態廣播。我認爲它會解決你的問題。

+0

why -ve你無法做到這一點。如果有任何問題寫在這裏.. –

相關問題