简体   繁体   English

如何将SQLite中的一个值(根据其他两个值)分配给我的Python变量?

[英]How do I assign a single value from SQLite (according to two other values) to my Python variable?

I'm trying to create a simple online login form using Python and SQLite3: 我正在尝试使用Python和SQLite3创建一个简单的在线登录表单:

user_email = request.forms.get("email")
user_password = request.forms.get("password")
connection = sqlite3.connect("/home/hassanrashid/mysite/users.db")
c = connection.cursor()
c.execute("SELECT UserName from account WHERE UserEmail=? AND UserPassword=?", (user_email, user_password))

The email and password values entered into the html form are assigned to the user_email and user_password variables respectively. 输入到html表单中的电子邮件和密码值分别分配给user_emailuser_password变量。 What I'm trying to do is, once the user logs in, he is greeted with a welcome message ("Welcome, UserName!"). 我要做的是,一旦用户登录,就会收到欢迎消息(“ Welcome,UserName!”)。

My question is, how do I assign the UserName value from my SQLite database table to my Python variable user_name ? 我的问题是,如何将SQLite数据库表中的UserName值分配给Python变量user_name

I've also tried this code: 我也尝试过以下代码:

user_name = c.execute("SELECT UserName from account WHERE UserEmail=?", (user_email)).fetchone()

But that gives me an error: "Incorrect number of bindings supplied. The current statement uses 1, and there are 15 supplied." 但这给了我一个错误: "Incorrect number of bindings supplied. The current statement uses 1, and there are 15 supplied." There are 15 rows in my database. 我的数据库中有15行。 I'm assuming that the criteria ( WHERE UserEmail=? ) isn't being applied. 我假设未应用标准( WHERE UserEmail=? )。

How do I fix this? 我该如何解决?

user_name = c.execute("SELECT UserName from account WHERE UserEmail=?", (user_email)).fetchone()

Two issues with this: 这有两个问题:

  1. (user_email) is not a tuple, so you are not passing a 1-element tuple to execute() . (user_email)不是元组,因此您没有将1元素元组传递给execute() Instead, pass (user_email,) to make a 1-element tuple. 而是传递(user_email,)组成一个1元素的元组。

  2. The return value of fetchone() will return the whole result row as a tuple. fetchone()的返回值将以元组的形式返回整个结果行。 If there is only one column in the row, you will still get a 1-element tuple back. 如果行中只有一列,您仍将返回一个1元素元组。 You need to unpack the tuple first in order to fix this: 您需要先打开元组的包装才能解决此问题:

query = "SELECT UserName from account WHERE UserEmail=?"
result = c.execute(query, (user_email,)) 

# either store the row tuple first
row = result.fetchone()
user_name = row[0]

# or you could unpack the tuple immediately (note the trailing comma)
user_name, = result.fetchone()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何根据第一次达到每组列中的最大值来分配值? - Python: How do I assign values according to first time reaching max value in column per group? 如何将变量分配给从 SQL 表中获取的值? (Sqlite 3 和 python) - How to assign variable to value fetched from SQL table? (Sqlite 3 and python) 如何根据变量值将变量分配给 python 中的不同值 - How do I assign a variable to different values in python, depending on the variable value 如何在Python中的两个列表的已建立循环中分配变量? - How do I assign a variable to an established loop of two lists in Python? 如何使用 Python 中的变量为 JSON 键赋值? - How do I assign a value to a JSON key using a variable in Python? 如何在 Python 中将字典值分配给变量? - How do I assign a dictionary value to a variable in Python? 如何使用我的 sqlite3 数据库中的表数据作为 python 变量 - How do i use table data from my sqlite3 database as a python variable 如何将文本输入中的值分配给kivy中的变量? - How do I assign the value from a text input to a variable in kivy? 使用Python和SQLite,如何将数据库中的项读入变量? - With Python and SQLite, how do i read an item from the database into a variable? 我如何从 sqlite3 表中 select 并将其分配给变量? - How do I select something from a sqlite3 table and assign it to a variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM