Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x (Python3)检查变量是否在sqlite3数据库中_Python 3.x_Sqlite - Fatal编程技术网

Python 3.x (Python3)检查变量是否在sqlite3数据库中

Python 3.x (Python3)检查变量是否在sqlite3数据库中,python-3.x,sqlite,Python 3.x,Sqlite,我正在为一个不和谐机器人编程。它将用户名和他们的钱保存在sqlite3数据库中。 现在我需要检查数据库中是否有用户名,以防止重复输入 money4 = 1000 def add_player(user): cursor.execute("INSERT INTO player VALUES (?,?)", (str(user), int(money4))) connection.commit() 这是将名称添加到数据库中的代码。有了它,人们可以得到双重输入

我正在为一个不和谐机器人编程。它将用户名和他们的钱保存在sqlite3数据库中。 现在我需要检查数据库中是否有用户名,以防止重复输入

money4 = 1000


def add_player(user):
    cursor.execute("INSERT INTO player VALUES (?,?)", (str(user), int(money4)))
    connection.commit()
这是将名称添加到数据库中的代码。有了它,人们可以得到双重输入,我只想阻止它。 编辑:数据库看起来像这样,有多个条目:

[('message.author', 1000), ('Micheltv10#9087', 1000), ('bempel#2040', 1000), ('bempel#2040', 1000), ('Micheltv10#9087', 1000), ('Micheltv10#9087', 1000), ('Micheltv10#9087', 1000), ('Micheltv10#9087', 1000), ('Micheltv10#9087', 1000)]

您可以在插入之前尝试选择用户名,以检查它是否已经存在。或者正确地执行此操作,并使用户名在数据库结构中唯一(可能作为主键),这样插入重复项会引发错误。

我通过创建一个带有主键的表来解决此问题

cursor.execute("""CREATE TABLE spieler(
            spielername TEXT PRIMARY KEY,
            money INTEGER
            )""")

我该怎么做?我可以使用什么功能