SQLite的:prevent重复 [英] SQLite: prevent duplicates

查看:169
本文介绍了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 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.

这篇关于SQLite的:prevent重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆