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

标签 python mysql sql

我是使用 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/

相关文章:

python - Sympy python 周长

python - 前瞻和非捕获正则表达式

php - 在重复键上插入...或更新...设置

mysql - 在更新中打开同一个表两次

mysql - 数据库 :how to separate sql queries coming from two tables?

python - 如何使用 Python 从 Github API 中的所有页面获取数据?

javascript - 从 API 数据切换 Div 隐藏/显示

mysql join消除重复

SQL 数据库设计最佳实践(地址)

mysql - SQL 从一列中选择不同的值,而另一列具有不同的值