如何检查android sqlite数据库中是否已存在该值?

时间:2021-11-12 22:36:54

I have application where user enters name and other details for specific date. I need to check whether value/name user is trying to add exists already in the database column name. I have this code but this doesn't work as it selects only one value. Thanks

我有应用程序,用户输入特定日期的名称和其他详细信息。我需要检查值/名称用户是否正在尝试添加已存在于数据库列名称中。我有这个代码,但这不起作用,因为它只选择一个值。谢谢

Cursor c = dbe.rawQuery("SELECT name FROM tbl_user WHERE meeting_date LIKE'" + mydate + "'", null);

if ((c.getString(3)).equals(editTextName.getText().toString()))

{
// do something
}

2 个解决方案

#1


2  

If you are only checking for existence of a value in a field, the easiest way is to do a select count with the appropriate filter.

如果您只检查字段中是否存在值,最简单的方法是使用适当的过滤器进行选择计数。

Something along the lines of

有点像

SELECT count(*)
FROM tbl_user
WHERE meeting_date LIKE ' + my_date'
AND name = ' + editTextName.getText() + '

this would allow you to easily and quickly determine if those value exist in your table.

这将允许您轻松快速地确定表中是否存在这些值。

If you need to check multiple tables, then you would have to do a join between those tables and write your filter accordingly.

如果需要检查多个表,则必须在这些表之间进行连接并相应地编写过滤器。

If by chance you don't have a good handle on SQL.
This site is a good starting place: W3Schools SQL Tutorial

如果碰巧你没有很好的处理SQL。这个网站是一个很好的起点:W3Schools SQL Tutorial

Hope this helps.

希望这可以帮助。

#2


1  

You are only selecting one column (name) and yet in your if you are trying to compare the contents of a (non-existent) third column (Not sure why you're trying to do that, but it's a problem, obviously).

你只是选择了一个列(名称),但是如果你试图比较一个(不存在的)第三列的内容(不知道为什么要尝试这样做,但显然这是一个问题)。

Rather than trying to do it that way, why not use the column title?

而不是试图这样做,为什么不使用列标题?

So your if would look like this (and it doesn't matter what position the column is in your cursor):

所以你的if看起来像这样(并且光标中列的位置无关紧要):

if (c.getString(c.getColumnIndex("name")).equals(editTextName.getText().toString()))

#1


2  

If you are only checking for existence of a value in a field, the easiest way is to do a select count with the appropriate filter.

如果您只检查字段中是否存在值,最简单的方法是使用适当的过滤器进行选择计数。

Something along the lines of

有点像

SELECT count(*)
FROM tbl_user
WHERE meeting_date LIKE ' + my_date'
AND name = ' + editTextName.getText() + '

this would allow you to easily and quickly determine if those value exist in your table.

这将允许您轻松快速地确定表中是否存在这些值。

If you need to check multiple tables, then you would have to do a join between those tables and write your filter accordingly.

如果需要检查多个表,则必须在这些表之间进行连接并相应地编写过滤器。

If by chance you don't have a good handle on SQL.
This site is a good starting place: W3Schools SQL Tutorial

如果碰巧你没有很好的处理SQL。这个网站是一个很好的起点:W3Schools SQL Tutorial

Hope this helps.

希望这可以帮助。

#2


1  

You are only selecting one column (name) and yet in your if you are trying to compare the contents of a (non-existent) third column (Not sure why you're trying to do that, but it's a problem, obviously).

你只是选择了一个列(名称),但是如果你试图比较一个(不存在的)第三列的内容(不知道为什么要尝试这样做,但显然这是一个问题)。

Rather than trying to do it that way, why not use the column title?

而不是试图这样做,为什么不使用列标题?

So your if would look like this (and it doesn't matter what position the column is in your cursor):

所以你的if看起来像这样(并且光标中列的位置无关紧要):

if (c.getString(c.getColumnIndex("name")).equals(editTextName.getText().toString()))