import pymysql
import time
class DataConnection:
host = '你的主机地址'
user = 'root'
passwd = '你的密码'
db = 'studentdata'
con = pymysql.connect(host=host, user=user, passwd=passwd, db=db)
cur = con.cursor()
n = 0
name = ''
def Register(self):#注册
sql = 'insert into user (name,passwd) values (%s,%s)'
self.name = input('账户:')
passwd = input('密码:')
parm = (self.name,passwd)
self.cur.execute(sql, parm)
self.con.commit()
def Delect(self):
self.sql = 'delete from user where name=%s'
str1 = input('输入要删除的:')
self.n = self.cur.execute(self.sql,str1)
self.con.commit()
def SignIn(self):
sql = 'select * from user where name=%s and passwd=%s'
self.name = input('账号:')
passwd = input('密码:')
parm = (self.name, passwd)
self.n = self.cur.execute(sql,parm)
self.con.commit()
class Determine:
frequency = 0
key_1 = '0'
key_2 = '1'
chose = ''
def Testing_1(self,cla):
self.key_1 = '1'
print('------------------------------')
print('登陆成功!Welcome %s' % (cla.name))
print('------------------------------')
def Testing_2(self):
print('------------------------------')
self.key_2 = input('退出请输入0\n继续请输入1\n')
print('------------------------------')
def Testing_3(self):
self.chose = input('注册输入1\n登录输入2\n删除输入3\n')
print('------------------------------')
对数据库studentdata的连接、增查删操作。
驱动函数文件如下
from dataconnection import *
if __name__ == '__main__':
A = DataConnection() #建立数据库连接类
B = Determine() #建立判断类
while B.key_2 == '1': #执行
B.Testing_3() #垃圾界面不值一提
if B.chose == '1': #注册
A.Register() #调用注册
B.Testing_2() #判断是否继续
elif B.chose == '2': #登录
A.SignIn() #调用登录
B.Testing_1(A) #判断登录次数,做出响应回复1
if B.key_1 == '1': #登陆成功
B.Testing_2() #判断是否继续
else: #登陆失败终止程序
break
elif B.chose == '3': #删除
A.Delect() #调用删除
B.Testing_2() #判断是否继续
else: #终止
print('无效指令!\n终止程序!')
break