如何更新数据库中的现有数据

时间:2016-10-07 15:32:57

标签: android sqlite android-sqlite

我需要创建一个像[this]这样的表:

enter image description here

我还想在创建表后更新现有记录。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您需要为SQLite数据库创建一个具有必要CRUD函数的DatabaseAdapter类。首先创建一个名为DatabaseAdapter.java的新Java类文件。插入以下代码:

public class DatabaseAdapter {
    private static final String databaseName  = "MealsDatabase";
    private static final int databaseVersion = 1;
    private static final String databaseTable = "Meals";
    private static final String columnId = "id";
    private static final String columnQuantity = "quantity";
    private static final String columnMeal = "meal";
    private static final String columnPrice = "price";
    private static final String columnAmount = "amount";

    DatabaseHelper helper;
    SQLiteDatabase database;

    public DatabaseAdapter(Context context) {
        this.helper = new DatabaseHelper(context, databaseName, databaseVersion);
    }

    public DatabaseAdapter open() throws SQLException {
        this.database = helper.getWritableDatabase();
        return this;
    }

    public void close() throws SQLException {
        this.helper.close();
    }

    public Cursor getAllMeals() {
        return this.database.query(databaseTable, new String[] {columnId, columnName, columnDate, columnDescription}, null, null, null, null, null);
    }

    public Cursor getMeal(long id) throws SQLException {
        Cursor cursor = this.database.query(true, databaseTable, new String[] {columnId, columnQuantity, columnMeal, columnPrice, columnAmount}, columnId + "=" + id, null, null, null, null, null);

        if (cursor != null) {
            cursor.moveToFirst();
        }
        return cursor;
    }

    public long insertMeal(int quantity, String meal, float price, int amount) {
        ContentValues contentValues = new ContentValues();

        contentValues.put(columnQuantity, quantity);
        contentValues.put(columnMeal, meal);
        contentValues.put(columnPrice, price);
        contentValues.put(columnAmount, amount);

        return this.database.insert(databaseTable, null, contentValues);
    }

    public boolean editMeal(long id, int quantity, String meal, float price, int amount) {
        ContentValues contentValues = new ContentValues();

        contentValues.put(columnQuantity, quantity);
        contentValues.put(columnMeal, meal);
        contentValues.put(columnPrice, price);
        contentValues.put(columnAmount, amount);

        return this.database.update(databaseTable, contentValues, columnId + "=" + id, null) > 0;
    }

    public boolean deleteMeal(long id) {
        return this.database.delete(databaseTable, columnId + "=" + id, null) > 0;
    }
}

现在,创建一个名为DatabaseHelper.java的新Java类,并添加以下代码:

public class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context, String databaseName, int databaseVersion) {
        super(context, databaseName, null, databaseVersion);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        try {
            database.execSQL("CREATE TABLE Meals (id INTEGER PRIMARY KEY AUTOINCREMENT, quantity INTEGER NOT NULL, meal TEXT NOT NULL, price REAL NOT NULL, amount INTEGER NOT NULL)");
        } catch (SQLException exception) {
            exception.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        Log.w("MealsDatabase", "Upgrading database from version " + oldVersion + " to " + newVersion + ".");
        database.execSQL("DROP TABLE IF EXISTS SavedLists");
        onCreate(database);
    }
}

在创建新餐的活动中,添加以下代码:

DatabaseAdapter database = new DatabaseAdapter(this);
database.open();

if (database.insertMeal(quantity, "Meal", 5.60, amount) > 0) {
    Toast.makeText(this, "A new meal has been created!", Toast.LENGTH_SHORT).show();
}

database.close();

要更新现有记录,请添加以下代码:

DatabaseAdapter database = new DatabaseAdapter(this);
database.open();

if (database.editMeal(id, quantity, "Meal", 5.60, amount)) {
    Toast.makeText(this, "A meal has been updated!", Toast.LENGTH_SHORT).show();
}

database.close();

您现在已经创建了您的膳食数据库,添加了一顿饭并更新了现有的膳食。