Python 查看sqlite表中是否已经存在这两个值

Python 查看sqlite表中是否已经存在这两个值,python,sql,sqlite,Python,Sql,Sqlite,我知道 检查是否有名称,但是否有方法检查表中是否已存在值1和值2 当然可以,您可以使用IN运算符,也可以使用或运算符。例如: cursor.execute("select * from table where value = ?",(user,)) 如果返回两行,则两行都存在 如果值不是唯一的(也就是说,user可能有20行,而otheruser可能没有),这个技巧不太管用,但您可以使用DISTINCT子句或GROUP BY轻松解决这个问题 此外,您可以只使用COUNT(*)而不是返回多行

我知道


检查是否有名称,但是否有方法检查表中是否已存在值1和值2

当然可以,您可以使用
IN
运算符,也可以使用
运算符。例如:

cursor.execute("select * from table where value = ?",(user,)) 
如果返回两行,则两行都存在

如果值不是唯一的(也就是说,user可能有20行,而otheruser可能没有),这个技巧不太管用,但您可以使用
DISTINCT
子句或
GROUP BY
轻松解决这个问题

此外,您可以只使用
COUNT(*)
而不是返回多行

因此:


您可以使用AND运算符

def both_exist(user, otheruser):
    cursor.execute("SELECT COUNT(*) FROM table WHERE value IN (?, ?) GROUP BY value", 
                   (user, otheruser))
    count, = cursor.fetchone()
    return count == 2
def both_exist(user, otheruser):
    cursor.execute("SELECT COUNT(*) FROM table WHERE value IN (?, ?) GROUP BY value", 
                   (user, otheruser))
    count, = cursor.fetchone()
    return count == 2
cursor.execute("SELECT * FROM table WHERE table_field1 = ? AND table_field2 = ?", (value1,value2))
result=cursor.fetchall()
if len(result)==0:
    print('Not found')
else:
    print('Found')