from MysqlHelper import MysqlHelper #导入mysql工具类
from datetime import datetime #导入日期模块
def add_stu():
# 1.实例化工具类
db = MysqlHelper('39.98.39.173', 13306, 'root', 'root', '1909C2')
# 2. 调用增加方法
#insert into students values(default,'张A','男',20,'2020-11-05','176','山西')
sql = 'insert into students values(default,%s,%s,%s,%s,%s,%s)'
params=['张D','男',20,datetime.now(),'176','山西']
num =db.my_execute(sql,params)
return num
def del_stu(name):
'''删除'''
db = MysqlHelper('39.98.39.173', 13306, 'root', 'root', '1909C2')
sql = 'delete from students where name =%s'
params=[name]
num = db.my_execute(sql,params) return num
def update_stu(oldName,name,age,sex,phone,addr):
db = MysqlHelper('39.98.39.173', 13306, 'root', 'root', '1909C2')
sql ='update students set name =%s,age=%s,sex=%s,phone=%s,addr=%s where name = %s'
params = (name,age,sex,phone,addr,oldName)
return db.my_execute(sql,params)
def get_all():
db = MysqlHelper('39.98.39.173', 13306, 'root', 'root', '1909C2')
sql = 'select * from students'
students = db.get_all(sql,None)
for stu in students:
print(f'名字:{stu[1]},性别:{stu[2]}')
def get_by_name(name):
db = MysqlHelper('39.98.39.173', 13306, 'root', 'root', '1909C2')
sql = 'select * from students where name =%s'
params = (name)
students = db.get_all(sql,params)
for stu in students:
print(f'名字:{stu[1]},性别:{stu[2]}')
if name == ' main ':
'''增加
if add_stu() > 0:
print('成功')
else:
print('失败')
'''
''' 删除
if del_stu('张D') >0:
print('成功')
else:
print('失败')
'''
'''修改
if update_stu('zhang',"张小三",40,'女','110110','陕西')>0:
print('修改成功')
else:
print('失败')
'''
get_all()