SQLite的:prevent重复SQLite、prevent

由网友(别拿装逼当典范)分享简介:我想创建一个表来存储设备的设置。该表有三行:ID,PARAMETER_NAME和PARAMETER_VALUE I would like to create a table to store device settings. The table has three rows: id, parameter_name a...

我想创建一个表来存储设备的设置。该表有三行:ID,PARAMETER_NAME和PARAMETER_VALUE

I would like to create a table to store device settings. The table has three rows: id, parameter_name and parameter_value.

该表是通过执行以下的查询语句创建的:

The table was created by executing the following query statement:

DATABASE_CREATE = "create table DATABASE_TABLE (KEY_ID INTEGER PRIMARY KEY AUTOINCREMENT, KEY_NAME INTEGER not null, VALUE TEXT not null);

再行通过执行以下方法保存:

and then the rows are stored by executing the following method:

private long insertRow(int rowParameter, String rowValue, SQLiteDatabase db){
    long res = -1;
    ContentValues settingsParameterValues = new ContentValues();
    settingsParameterValues.put(KEY_NAME, rowParameter);
    settingsParameterValues.put(VALUE, rowValue);
    if(db != null){
        res = db.insert(DATABASE_TABLE, null, settingsParameterValues);
    }
    return res;
}

在创建数据库的默认值存储:

When the database is created the default values are stored:

@Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
        insertRow(PRIVACY_LEVEL_ROW_INDEX, "0", db);
        insertRow(STREAM_TITLE_ROW_INDEX, "untitled", db);
        insertRow(STREAM_TAGS_ROW_INDEX, "", db);
    }

与方法的insertRow()的问题然而是,它不能prevent复制的条目。

The problem with method insertRow() however is that it can't prevent duplicating the entries.

有谁知道如何$这个字母p $ pvent重复的项目?

Does anyone know how to prevent duplicate entries in this case?

推荐答案

可以使用列约束唯一

UNIQUE约束导致要在指定的列创建唯一索引。

The UNIQUE constraint causes an unique index to be created on the specified columns.

阅读全文

相关推荐

最新文章