|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码如下:
- import sqlite3
- from PyQt5 import QtWidgets
- from PyQt5.QtWidgets import QWidget
- from PyQt5.QtWidgets import *
- from Mainview.Login import Ui_Login
- from Mainview.Register import Ui_Register
- from Student.StudentClass import Sc
- import pymysql
- from Mainview.sqltool import sqlInsert, register, sqlDelete, sqlChange, findSql, sqlnum
- class Run(Ui_Login,QWidget):
- def __init__(self):
- super(Run,self).__init__()
- self.setupUi(self)
- #信号槽
- self.Loading.clicked.connect(self.close)
- self.register_2.clicked.connect(self.close)
- self.exit.clicked.connect(self.close)
- self.viewpwd.clicked.connect(self.passit)
- def passit(self):
- if self.password.echoMode() == QLineEdit.Password:
- self.password.setEchoMode(QLineEdit.Normal)
- else:
- self.password.setEchoMode(QLineEdit.Password)
- class Run_sc(Sc, QMainWindow):
- def __init__(self):
- super(Run_sc, self).__init__()
- self.setupUi(self)
- self.pushButton.clicked.connect(self.close)
- class Run_re(Ui_Register, QMainWindow):
- def __init__(self):
- super(Run_re, self).__init__()
- self.setupUi(self)
- self.cancel.clicked.connect(self.close)
- self.Register_2.clicked.connect(self.Register)
- def Register(self):
- username_1 = self.username.text()
- passwd_1 = self.passwd.text()
- recpd = self.recpd.text()
- if not passwd_1 or not recpd: # 如果有一个密码或者密码确认框为空
- QMessageBox.information(self, 'Error', 'The password is empty', QMessageBox.Yes)
- elif self.is_has(username_1): # 如果用户名已经存在
- QMessageBox.information(self, 'Error', 'The username already exists', QMessageBox.Yes)
- else:
- if passwd_1 == recpd and passwd_1: # 如果两次密码一致,并且不为空
- connect = pymysql.connect(
- host='localhost',
- user='root',
- passwd='123456',
- database='scCollege',
- charset='utf8'
- )
- cursor = connect.cursor()
- sql = "insert into userinfo(username_1, passwd) values (%s, %s);"
- cursor.execute(sql, [username_1, passwd_1])
- connect.commit()
- cursor.close()
- connect.close()
- QMessageBox.information(self, '成功!', '注册成功!'.format(username_1),QMessageBox.Yes)
- self.close() # 注册完毕之后关闭窗口
- else:
- QMessageBox.information(self, '错误!', 'The password is not equal', QMessageBox.Yes)
- def is_has(username):
- """判断数据库中是否含有用户名"""
- connect = pymysql.connect(
- host='localhost',
- user='root',
- passwd='123456',
- database='scCollege',
- charset='utf8'
- )
- cursor = connect.cursor()
- sql = "select * from userinfo where username_1 = %s"
- result = cursor.execute(sql, [username])
- connect.commit()
- data = cursor.fetchall() # 获取所有的内容
- cursor.close()
- connect.close()
- if data:
- return True
- else:
- return False
- def closeEvent(self, event):
- # 关闭之后将输入框清空
- self.username.setText('')
- self.recpd.setText('')
- self.passwd.setText('')
- if __name__=="__main__":
- #1、创建一个应用
- import sys
- app=QtWidgets.QApplication(sys.argv)
- ui=Run()
- ui2=Run_sc()
- ui3 = Run_re()
- ui.show()
- # ui.login()
- # ui.Register()
- ui.Loading.clicked.connect(ui2.show)
- ui.register_2.clicked.connect(ui3.show)
- ui3.cancel.clicked.connect(ui.show)
- #5、启动主循环
- sys.exit(app.exec_())
复制代码
但是会出现如下错误
求大神帮忙,谢谢
连接数据库有不同的写法,根据你这里的我稍微改一下,应该可以看得懂了。
ps: 可以参考官网的写法,用with语句更方便、简洁。
- import pymysql
- def is_has(user_input):
- """判断数据库中是否含有用户名"""
- connect = pymysql.connect(
- host='localhost',
- user='root',
- passwd='123456',
- database='mydb',
- charset='utf8'
- )
- cursor = connect.cursor()
-
- sql = "select * from score where name = %s"
- cursor.execute(sql, [user_input])
- # connect.commit() # 非DML语句无序commit()。insert, update, delete这样的DML语句才行考虑commit()
- data = cursor.fetchall() # 获取所有的内容
- cursor.close()
- connect.close()
- if data:
- return True
- else:
- return False
- if __name__ == "__main__":
- stu_name = input("输入要查找的学生名:")
- print(is_has(user_input=stu_name))
复制代码
|
|