You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.8 KiB
79 lines
1.8 KiB
import pymysql
|
|
|
|
|
|
# conn = pymysql.connect(host="IP地址", port=端口号, user="用户名", password="密码", db="数据库名")
|
|
con = pymysql.connect(host='localhost', port=3306, user="root", password="passwd", db="jzw_text")
|
|
cursor = con.cursor()
|
|
#cursor.execute("create table xs(name char(5),sex char(5),tel char(30));")
|
|
|
|
def zhixing():
|
|
cursor.close()
|
|
con.commit()
|
|
|
|
#增加
|
|
def insert(li):
|
|
cursor.execute("insert into xs values(%s,%s,%s);", li) # # 插入数据
|
|
zhixing()
|
|
#删除
|
|
def delete(li):
|
|
cursor.execute("delete from xs where name=(%s);",li)
|
|
zhixing()
|
|
print("已删除{}的信息".format(li))
|
|
|
|
#查询
|
|
def seeion(li):
|
|
cursor.execute("select * from xs where name=(%s);",li)
|
|
# 获取查询结果
|
|
results = cursor.fetchall()
|
|
# 打印输出结果
|
|
for row in results:
|
|
print(row)
|
|
zhixing()
|
|
|
|
#修改
|
|
def update(li):
|
|
cursor.execute("update xs set name=%s,sex=%s,tel=%s",li)
|
|
zhixing()
|
|
|
|
#显示所有学员
|
|
def xians():
|
|
cursor.execute("select * from xs;")
|
|
results = cursor.fetchall()
|
|
# 打印输出结果
|
|
for row in results:
|
|
print(row)
|
|
zhixing()
|
|
|
|
def close():
|
|
cursor.close()
|
|
con.commit()
|
|
con.close() # 最后提交所有操作,并关闭游标和连接
|
|
print("选择如下功能:")
|
|
print("1:添加学员\n2:删除学员\n3:修改学员信息\n4:查询学员\n5:显示所有学员信息\n6:退出系统")
|
|
a=int(input())
|
|
if a==1:
|
|
m=[]
|
|
m.append(input())
|
|
m.append(input())
|
|
m.append(input())
|
|
insert(li=m)
|
|
elif a==2:
|
|
m = input()
|
|
delete(m)
|
|
elif a==3:
|
|
m = []
|
|
m.append(input())
|
|
m.append(input())
|
|
m.append(input())
|
|
update(m)
|
|
|
|
elif a ==4:
|
|
m = input()
|
|
seeion(m)
|
|
|
|
elif a==5:
|
|
xians()
|
|
elif a==6:
|
|
close()
|
|
|