Check if row with value already exists in SQLite table

How do I check to see if row with a particular value already exists in a SQLite table?

My table has a String "name" column. I would like to check out if a particular name already exists in the another table row. I am still learning SQLite, but I think it is possible to do this with the cursor.


If you have a UNIQUE constraint on the name column, you can use INSERT OR IGNORE (or insertWithOnConflict() in Android) to insert a row only if it does not already exist.

In the general case, to check whether a row exists, you have to run a SELECT query. However, there is a helper function for counting rows:

boolean nameExists(String name) {
    SQLiteDatabase db = ...;
    long count = DatabaseUtils.queryNumEntries(db,
                    "MyTable", "name = ?", new String[] { name });
    return count > 0;
}
链接地址: http://www.djcxy.com/p/19808.html

上一篇: 如何检查一个表是否存在于android sqlite中

下一篇: 检查SQLite表中是否存在具有值的行