SQLiteDatabase:将仅当值不存在(不通过原始的SQL命令) [英] SQLiteDatabase: Insert only if the value does not exist (not via raw SQL command)

查看:127
本文介绍了SQLiteDatabase:将仅当值不存在(不通过原始的SQL命令)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有是这样写的SQL命令: IF NOT EXISTS ,但由于Android的 SQLiteDatabase 类有一些细微的方法,我想知道是否有可能插入值,如果它没有通过的方法存在。

目前我使用此项插入字符串

 众长insertString(字符串键,字符串值){
    ContentValues​​ initialValues​​ =新ContentValues​​();
    initialValues​​.put(键,值);
    返回db.insert(DATABASE_TABLE,空,initialValues​​);
}
 

分贝是一个实例 SQLiteDatabase

我试过的方法 insertOrThrow()而不是插入(),但似乎它一样插入()。也就是说,如果该值已经是行中,它的再次插入这样的行现在具有相同的值的两个值

解决方案
  

SQLiteDatabase:插入仅当该值不经由原料存在(未   SQL命令)

既然你不想使用原始的查询就可以实现它在插入之前,刚刚创建一些功能,这将考验,如果值已经在数据库中。它可以返回布尔(或INT),如果它返回false,您将执行INSERT查询。

一个小例子:

 公众诠释getCount将(){
    光标C = NULL;
    尝试 {
        DB = Dbhelper.getReadableDatabase();
        ?从表名,其中name = SELECT COUNT(*)字符串查询=;
        C = db.rawQuery(查询,新的String [] {名});
        如果(c.moveToFirst()){
            返回c.getInt(0);
        }
        返回0;
    }
    最后 {
        如果(C!= NULL){
            c.close();
        }
        如果(DB!= NULL){
            db.close();
        }
    }
}

如果(getCount将()== 0){
   //执行插入
}
 

  

如果该值已经是行中,它的再现在插入,使得该行   具有相同的值的两个值

这可以通过适当的限制,不会允许你插入重复的使用情况来解决。检查:

I know there's an SQL command that goes like this: IF NOT EXISTS, but since Android's SQLiteDatabase class has some fine methods, I was wondering if it's possible to insert a value if it doesn't exist via a method.

Currently I'm using this to insert a String:

public long insertString(String key, String value) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(key, value);
    return db.insert(DATABASE_TABLE, null, initialValues);
}

(db is an instance of SQLiteDatabase.)

I tried the method insertOrThrow() instead of insert(), but it seems it does the same as insert(). That is, if the value is already in the row, it's inserted again so the row now has two values of the same value.

解决方案

SQLiteDatabase: Insert only if the value does not exist (not via raw SQL command)

Since you don't want to use raw queries you can achieve it that before inserting, just create some function that will test if value is already in database. It could return boolean(or int) and if it return false, you will perform insert query.

A little example:

public int getCount() {
    Cursor c = null;
    try {
        db = Dbhelper.getReadableDatabase();
        String query = "select count(*) from TableName where name = ?";
        c = db.rawQuery(query, new String[] {name});
        if (c.moveToFirst()) {
            return c.getInt(0);
        }
        return 0;
    }
    finally {
        if (c != null) {
            c.close();
        }
        if (db != null) {
            db.close();
        }
    }
}

if (getCount() == 0) {
   //perform inserting
}

if the value is already in the row, it's inserted again so the row now has two values of the same value.

This can be solved by an usage of proper constraints which won't allow you to insert duplicates. Check this:

这篇关于SQLiteDatabase:将仅当值不存在(不通过原始的SQL命令)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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