python - 如何继续添加、检索和比较 MySQL 数据库中的数据?我正在尝试做一个登录平台
问题描述
我正在尝试制作一个登录系统。
- 用户注册。
- 用户名和密码存储在数据库中
- 用户尝试登录
- 如果成功,代码给出“登录!” 信息。
这是代码:
import mysql.connector as msqltor
db = msqltor.connect(
host='localhost',
user='root',
passwd='qwerty',
database='test_db'
)
my_cursor = db.cursor()
def signup():
username = input("Enter username: ")
password = input("Enter password: ")
Q2 = "INSERT INTO credentials(username, password) VALUES (%s, %s)", (username, password)
my_cursor.execute(Q2)
db.commit()
def login():
username = input("Enter username: ")
password = input("Enter password: ")
credentials_tuple = (username, password)
Q3 = "SELECT * FROM credentials where username=%s"
my_cursor.execute(Q3, (username))
if credentials_tuple == my_cursor:
print("Logged in!")
else:
print("Error")
Q1 = '''CREATE TABLE credentials(
username varchar(25),
password varchar(25)
)'''
message = '''Welcome to login: \n
1. Signup \n
2. Login \n
'''
user_input = int(input(message))
if user_input == 1:
signup()
elif user_input == 2:
login()
# my_cursor.execute(Q1)
我收到此错误消息:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 486, in cmd_query
query = query.encode('utf-8')
AttributeError: 'tuple' object has no attribute 'encode'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "login.py", line 47, in <module>
signup()
File "login.py", line 17, in signup
my_cursor.execute(Q2)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/cursor_cext.py", line 266, in execute
raw_as_string=self._raw_as_string)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 499, in cmd_query
errno=2055, values=(addr, 'Connection not available.'))
mysql.connector.errors.OperationalError: 2055: Lost connection to MySQL server at 'localhost:3306', system error: Connection not available.
查看错误,似乎没有连接到 MySQL 数据库。但是,我认为这不是问题,因为我已确保在代码开头进行连接。
解决方案
这些行是问题所在:
Q2 = "INSERT INTO credentials(username, password) VALUES (%s, %s)", (username, password)
my_cursor.execute(Q2)
您想像这样将查询和值传递给游标的execute
方法
Q2 = "INSERT INTO credentials(username, password) VALUES (%s, %s)"
my_cursor.execute(Q2, (username, password))
你可以这样做:
Q2 = "INSERT INTO credentials(username, password) VALUES (%s, %s)", (username, password)
my_cursor.execute(*Q2)
但它并不常见,所以我会避免它。
发生的事情是
Q2 = "INSERT INTO credentials(username, password) VALUES (%s, %s)", (username, password)
创建一个tuple
, 并将mycursor.execute
此元组视为它的第一个参数,而不是将其拆分为查询和值部分,这会导致
AttributeError: 'tuple' object has no attribute 'encode'
在登录功能中,这段代码有问题:
Q3 = "SELECT * FROM credentials where username=%s"
my_cursor.execute(Q3, (username))
if credentials_tuple == my_cursor:
传递给的 values 参数mycursor.execute
必须是 a tuple
,如下所示:
my_cursor.execute(Q3, (username,)) # <- note the comma
然后您必须获取执行结果以进行比较:
if credentials_tuple == my_cursor.fetchone():
推荐阅读
- postgresql - 非唯一索引一起使用时如何强制“postgresql”使用唯一索引
- nativescript - StackLayout 的垂直对齐方式
- command-line-interface - Wildfly 14 CLI 编辑默认 jgroups
- c# - nullcheck 真的适用于 statebag 吗?
- ios - IOS Objective-c - 获取联系人不起作用 - 没有请求提示 - 访问被拒绝
- jquery - 滑块未添加到剑道网格中!列锁不起作用
- kiwi-tcms - Kiwi TCMS 说当我“报告”Github 问题时我需要填写 base_url,即使该字段已填充
- java - 通知通道 - 设置后是否可以更改 LightColor?
- python - Tensorflow批处理:将结果保存为字符串
- java - 避免简单的 if/else 条件