如何在不丢失旧数据的情况下在sqlite数据库上添加数据?

时间:2014-04-07 05:03:29

标签: android sqlite upgrade

现在我发布了我的报价应用程序,它有一个收藏夹报价部分。用户可以向收藏夹添加任何报价,但如果我在数据库中添加一些引号并在Google Play上更新应用程序,则当用户更新它们时会丢失收藏夹,就好像旧数据库已删除一样

他们如何更新它以获得新的报价而不会丢失他们的收藏夹?

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 个答案:

答案 0 :(得分:1)

当用户从Play商店更新您的应用时,您必须按照以下步骤操作: -

1-将您喜爱的数据库从db备份到temp arraylist   2-删除旧数据库。
  3-创建新数据库并在新数据库中插入记录。

注意: - 如果进程很大,那么使用IntentService和静态广播。我认为它将解决您的问题。