已经做好了一个界面并完成了注册功能,但无法成功登录
这是注册代码
def get_Mysql_conn(self):
try:
config = {
"host": "localhost",
"user": "root",
"password": "zhaoqifeng159",
"database": "adimin"
}
self.conn = pymysql.connect(**config)
except pymysql.Error as e:
print('Error: %s' % e)
def close_Mysql_conn(self):
try:
if self.conn:
self.conn.close()
except pymysql.Error as e:
print('Error: %s' % e)
def get_Mysql_userinfo(self):
self.get_Mysql_conn()
sql = ' SELECT * FROM userinfo' #这里我的登录用户头叫userinfo ,每个人的数据库不同,在建立时初始化。
# 使用cursor()方法获取操作游标
cursor = self.conn.cursor()
# 使用execute()方法执行SQL语句
cursor.execute(sql)
# 使用fetchall()方法获取全部数据
result = cursor.fetchall()
# 将数据用字典形式存储于result
result = [dict(zip([k[0] for k in cursor.description], row)) for row in result]
# 关闭连接
cursor.close()
self.close_Mysql_conn()
return result
def insert_Mysql_userinfo(self, text):
print("进入用户注册提交函数")
self.name = str(self.lineEdit_3.text())
self.pwd = str(self.lineEdit_4.text())
self.get_Mysql_conn()#连接数据库
sql = 'SELECT * FROM userinfo'
cursor = self.conn.cursor()
cursor.execute(sql)
result = cursor.fetchall()
print("result=", result)
result = [dict(zip([k[0] for k in cursor.description], row)) for row in result]
ulist = []
for item in result:
ulist.append(item['name'])
print("ulist=", ulist)
try:
sql = "insert into userinfo(name, pwd) values('{}', '{}');".format(self.name,self.pwd)
cursor.execute(sql)
if self.name == '' or self.pwd == '':
self.conn.rollback()
QMessageBox.warning(self, "警告", "账号或者密码含有空格,注册失败", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
elif self.name in ulist:
#messagebox.showerror('警告', message='用户名已存在')
#msg_box = QMessageBox(QMessageBox.Warning, '警告', '用户名已存在')
QMessageBox.warning(self, "警告", "用户名已存在", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
else:
# 提交事务
self.conn.commit()
#messagebox.showinfo(title='恭喜', message='注册成功')
#msg_box = QMessageBox(QMessageBox.information, '恭喜', '注册成功')
QMessageBox.information(self, "欢迎", "注册成功", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
mainface.close() # 关闭注册界面,在这之后就可以进入你编写的其他界面了。
cursor.close()
self.close_conn()
except:
# 限制提交
self.conn.rollback()
这是登录代码
def on_pushButton_Login_clicked(self, text):
user_name = self.lineEdit.text()
user_password = self.lineEdit_2.text()
print("进入账号判断函数")
self.get_Mysql_conn() # 连接数据库
# 获取数据库中的值
sql = 'SELECT * FROM userinfo'
cursor = self.conn.cursor()
cursor.execute(sql)
results=cursor.fetchall()
print(results)
name=[]
password=[]
for row in results:
adimin_nmae=row[0]
name.append(adimin_nmae)
admin_password=row[1]
password.append(admin_password)
print(name)
print(password)
# 判断是否存在该账号
flag=0
print(type(self.lineEdit.text()))
for i in range(len(name)):
if user_name == name[i] and user_password == [i]:
flag=1
else:
pass
# print(type(name[i]))
# print(type(self.lineEdit.text()))
if flag == 1:
self.pushButton_3.clicked.connect(self.logindef)
else:
QMessageBox.question(self, "警告", "用户名或密码不正确", QMessageBox.Yes)
这是数据库
登录上明明输入正确还是显示不正确
######有没有好心人修正一下,或者写一个新的登录的方法