gpt4 book ai didi

python - 使用变量 python 从 mysql 读取/存储

转载 作者:行者123 更新时间:2023-11-29 12:22:29 26 4
gpt4 key购买 nike

我是使用 mysql 的新手,我正在尝试从数据库中检索并将其打印/存储在变量中。到目前为止我已经这样做了:

import mysql.connector

conn = mysql.connector.connect(database = 'UserInfo')

mycursor = conn.cursor()


Username = "Max"

Login = ("SELECT username, password FROM UserInfo.User "
"WHERE username = '%s'")
mycursor.execute(Login, (Username))

print((username, password) in mycursor)

mycursor.close()
conn.close()

最佳答案

您需要使用fetchone()fetchall()来检索结果:

username = "Max"
query = """
SELECT
username, password
FROM
UserInfo.User
WHERE
username = %s
"""

mycursor.execute(query, (username, ))
username, password = mycursor.fetchone() # here we can unpack the tuple returned from fetchone

关于python - 使用变量 python 从 mysql 读取/存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28819024/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com