作业
import pymysql
# tkinter模块是Python的标准Tk GUI工具包的接口
from tkinter import *
from tkinter import messagebox
# 连接数据库
class MysqlSearch(object):
def __init__(self):
self.get_conn()
# 获取连接
def get_conn(self):
try:
self.conn = pymysql.connect(
host='localhost',
user='root',
passwd='020920',
db='mysql',
charset='utf8')
except pymysql.Error as e:
print('Error: %s' % e)
# 关闭连接
def close_conn(self):
try:
if self.conn:
self.conn.close()
except pymysql.Error as e:
print('Error: %s' % e)
# 获取用户信息(登录用)
def get_userinfo(self):
sql = 'SELECT * FROM stu_information'
# 使用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_conn()
return result
# 注册
def insert_userinfo(self,a,b,c):
self.a = a
self.b = b
self.c = c
sql = 'SELECT * FROM stu_information'
cursor = self.conn.cursor()
cursor.execute(sql)
result = cursor.fetchall()
result = [dict(zip([k[0] for k in cursor.description],row)) for row in result]
ulist = []
for item in result:
ulist.append(item['姓名'])
try:
# sql = 'INSERT INTO 登陆账户(用户名,班级,学号) VALUES(%s,%s,%s)'
cursor = self.conn.cursor()
cursor.execute('INSERT INTO stu_information(姓名,班级,学号) VALUES(%s,%s,%s)',(self.a,self.c,self.b))
if self.a == '' or self.b == '' or self.c == '':
self.conn.rollback()
messagebox.showerror('警告',message = '注册失败')
elif self.a in ulist:
messagebox
浙公网安备 33010602011771号