Android的SQL - 检查整行已存在于数据库 [英] Android SQL - Check if whole row already exists in database
问题描述
我想创建一个简单的收藏夹的应用程序,在那里你可以保持您的收藏夹页面(从网站为例)。
I'm trying to create a simple favorites application, where you can keep your favorites pages (from the web for example).
让我们说我有以下数据在我的数据库
Let's say I have the following data in my database
Title | URL | TAG |
-------------------------------
Hey Ho.com Site
Jo Jo.com Image
Jo Mo.com Image
现在我要确保,在用户不添加同一页面两次,所以我会检查该值已经存在。但我需要的用户没有一个网站添加到已添加的表。
Now I want to make sure, the user does not add the same page twice, so I will check if the value already exists. But I need to that the user does not add a site to the table that has already been added.
现在,让我们说,我会尝试添加:
So let's say I would try to add:
Title | URL | TAG |
-------------------------------
Jo Mo.com Image
这会返回true(即检查是否存在排,因为一个相同的行已存在)。
This it would return true (for "check if row exist", because an identical row already exists).
但是,如果我会尝试添加:
But if I would try to add:
Title | URL | TAG |
-------------------------------
Jo Go.com Image
有将返回false(虽然标题已经存在),因为没有相同行
It would return false (though the title already exists), because there is no identical row.
这是在code其中添加我的数据到数据库:
This is the code where I add my data to the database with:
public long createNote(String title, String url, String rn) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_URL, url);
initialValues.put(KEY_RN, rn);
return mDb.insert(DATABASE_TABLE, null, initialValues);
我如何检查是否已存在某行的数据库?
How can I check if a row already exists in a database?
推荐答案
您可以检查使用光标:
public boolean checkEvent(String title, String URL, String tag)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLES,
new String[] { KEY_TITLE,KEY_URL,KEY_TAG },
KEY_TITLE + " = ? and "+ KEY_URL + " = ? and " + KEY_TAG + " = ?" ,
new String[] {title,url,tag},
null, null, null, null);
if(cursor.moveToFirst())
return true; //row exists
else
return false;
}
编辑:code现在复制和;粘贴准备
Code now copy & paste ready
这篇关于Android的SQL - 检查整行已存在于数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!