sqlite如何在冲突上添加值

时间:2013-07-06 08:48:42

标签: java android sqlite

我有一个包含产品名称,产品价格和产品计数器的数据库。 产品名称是唯一的,每次输入新值时产品价格都会被替换,问题是产品计数器。 其默认值为1,当输入新产品时,其值设置为1.如果产品名称发生冲突,我需要增加它。所以,如果输入Cheese两次,则counter会说2等等。 我需要它做的是当发生冲突时,将其值加1。我想这样做,因为我稍后会需要它。我需要在我计划在我的应用程序中实现的其他东西上将输入值添加到表值。

我怎样才能做到这一点?我想按照我现在的方式继续这样做,使用contentvalues方法插入而不是使用sqlite语法(INSERT,SELECT,etc)。这甚至可能吗?因为我在sqlite语法中是绝对的0。而且,我需要它有一个方法,我可以调用其他活动插入数据库(如insertCountry(Japan,10)

public class SQLiteCountryAssistant extends SQLiteOpenHelper {
private static final String DB_NAME = "usingsqlite.db";
private static final int DB_VERSION_NUMBER = 1;
private static final String DB_TABLE_NAME = "countries";
private static final String DB_COLUMN_1_NAME = "country_name";
private static final String DB_COLUMN_2_NAME = "country_price";
private static final String DB_COLUMN_3_NAME = "country_counter";


private static final String DB_CREATE_SCRIPT = "create table "
        + DB_TABLE_NAME
        + " (_id integer primary key autoincrement, country_name text UNIQUE ON CONFLICT REPLACE,country_price text,country_counter integer default '1' );)";

这就是我插入的方式:

public void insertCountry(String countryName, String countryPrice) {
    sqliteDBInstance.execSQL("INSERT INTO " + DB_TABLE_NAME
            + "(country_name, country_price) VALUES('" + countryName
            + "', '" + countryPrice + "')");

}

2 个答案:

答案 0 :(得分:0)

我不知道您的数据库类,但请检查this method

return this.sqliteDBInstance.insertWithOnConflict(DB_TABLE_NAME, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);

如果您正确创建列,如果存在冲突,则新条目将替换旧条目。

编辑:在您上次评论之后,您只需要更新:首先使用您的项目名称查询您的数据库(仔细检查参数是否正常):

return this.sqliteDBInstance.query(DB_TABLE_NAME, null, DB_COLUMN_1_NAME + "=?", new String[] {productName}, null, null, null);

这将返回一个0或1行的Cursor。如果没有行,您可以继续正常插入数据(不要忘记添加您的计数器:首次插入时为1):

public void insertCountry(String countryName, String countryPrice) {
sqliteDBInstance.execSQL("INSERT INTO " + DB_TABLE_NAME
        + "(country_name, country_price) VALUES('" + countryName
        + "', '" + countryPrice + "', '" + countryCounter + "')");

}

如果有1行,那么您的产品已经在您的数据库中,所以只需迭代Cursor,获取计数器上的值,在其上添加+1并使用this method更新您的数据库:

public int update (String table, ContentValues values, String whereClause, String[] whereArgs)

答案 1 :(得分:0)

在sqlite中无法将值增加到特定单元格。您必须读取单元格的当前值并向其添加所需的值并将其替换为旧值。您可以使用更新方法。

public void update(String countryName, String price, long id) {
    ContentValues cv = new ContentValues();
    cv.put(dbHelper.COLUMN_1, countryName); // These Fields should be your
                                        // String values of actual column
                                        // names
    cv.put(dbHelper.COLUMN_2, price);
    database.update(dbHelper.TABLE_NAME, cv, "_id " + "=" + id, null);
}

每次你要在你的表中添加一行时你必须读取它并检查该行是否存在。这是一种检索所有行的方法:

public List<TableRow> getAllRows() {
    List<TableRow> rows= new ArrayList<TableRow>();

    Cursor cursor = database.query(Helper.TABLE_SITUPS, allColumns,
            null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        TableRow row = cursorToRow(cursor);
        comments.add(row);
        cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return rows;
}

TableRow是数据库表行的类,包含实际表列的字段。 通过迭代此列表并获取每个列表的“country”值,您可以理解该行是否存在。

这些是sqlite数据库编程的基本概念。我建议你在这件事上研究一下。