用到的软件phpstudy、pycharm、Navicat Premium12
phpstudy是什么?
phpstudy是一个php运行环境的集成包,用户不需要去配置运行环境,就可以使用,phpstudy不仅是一款比较好用的php调试环境工具,并且还包括了开发工具和常用手册,对于新手是有很大帮助的。
Navicat简介
Navicat是一套快速、可靠的数据库管理工具,专为简化数据库的管理及降低系统管理成本而设。它的设计符合数据库管理员、开发人员及中小企业的需要。Navicat 是以直觉化的图形用户界面而建的,让你可以以安全并且简单的方式创建、组织、访问并共用信息。
本篇文章需要启动MySQL5.7.26,如下图所示:
上面phpstudy的用户名和密码就对应了下面的Navicat的用户名和密码
这里的连接名可以自己取,主机和端口保持默认就可以
本篇文章以stu表为例,写入学生信息
以下是自写模块:
# coding=utf-8
import sys
import pymysql
class Mysql_python(object):
#定义构造函数,定义链接数据库所需要的数据
def __init__(self,host,username,userpass,port,charset,dbname):
self.host_name=host
self.uname=username
self.upass=userpass
self.uport=port
self.char_set=charset
self.db_name=dbname
#链接数据库操作,使用构造函数中的数据进行与数据库得到链接
def connect(self):
self.con=pymysql.connect(
host=self.host_name,
user=self.uname,
password=self.upass,
port=self.uport,
charset=self.char_set,
db=self.db_name
)
#创建一个游标
def cursor(self):
self.cur=self.con.cursor()
#关闭游标以及数据库
def closs(self):
self.cur.close()
self.con.close()
#定义对数据进行增/删/改操作的函数
def sql_edit(self,sql):
try:
self.connect()
self.cursor()
self.num=self.cur.execute(sql)
except Exception as err:
print(err)
else:
self.con.commit()
self.closs()
return self.num
#定义对数据进行查询的函数
def sql_select(self,sql):
try:
self.connect()
self.cursor()
self.cur.execute(sql)
except Exception as err:
print(err)
sys.exit()
else:
num=self.cur.fetchall()
self.closs()
return num
以下是主程序:
导入自写模块stu(本篇的py文件就为stu)
#coding=utf-8
import stu
# 链接数据库
host="localhost"
# 用户名
user="root"
# 登录密码
pswd="xxx"
# 数据库端口号
port=3306
# 编码类型
charset="utf8"
# 选择数据库
db="stu"
obj=stu.Mysql_python(host,user,password=pswd,port,charset,db)
obj.connect()
def addStu():
n=1
while True:
stuid=input("请输入学生学号:").strip()
stuname=input("请输入学生姓名:").strip()
stuage=input("请输入学生年龄:").strip()
stutel=input("请输入学生电话:").strip()
if len(stuid)==0 or len(stuname)==0 or len(stuage)==0 or len(stutel)==0:
print("不能输入为空")
else:
sql1 = f"select stuid from stu1 where stuid={int(stuid)}"
obj.sql_select(sql1)
if n <= 3:
if obj.sql_select(sql1):
print("学号已存在,请勿重复添加")
n += 1
else:
sql2 = f"insert into stu1(stuid,name,age,tel) values({int(stuid)},'{stuname}',{int(stuage)},{int(stutel)})"
try:
obj.sql_edit(sql2)
except Exception as err:
print(f"错误信息如下{err}")
else:
try:
userInput = input("添加成功是否继续添加[y|n]").lower().strip()
except Exception as err:
print(err)
else:
if userInput == "y":
n += 1
continue
elif userInput == "n":
break
else:
break
else:
print("输入次数达到上限")
break
def deleteStu():
stuid = input("请输入学号:").strip()
if len(stuid) == 0:
print("输入的学号不能为空,请重新选择")
else:
sql1 = f"select stuid from stu1 where stuid={int(stuid)}"
obj.sql_select(sql1)
if obj.sql_select(sql1):
sql2 = f"delete from stu1 where stuid={int(stuid)}"
try:
obj.sql_edit(sql2)
except Exception as err:
print(f"错误信息如下:{err}")
else:
print("删除成功")
else:
print("学生不存在")
def selectStu():
stuid = input("请输入学号:").strip()
if len(stuid) == 0:
print("输入的学号不能为空,请重新选择")
else:
sql1 = f"select stuid from stu1 where stuid={int(stuid)}"
obj.sql_select(sql1)
if obj.sql_select(sql1):
sql2 = f"select * from stu1 where stuid={int(stuid)}"
res = obj.sql_select(sql2)
print(f"学生的学号是{res[0][1]}")
print(f"学生的姓名是{res[0][2]}")
print(f"学生的年龄是{res[0][3]}")
print(f"学生的电话是{res[0][4]}")
else:
print("学生不存在")
def editStu():
stuid = input("请输入学号:").strip()
if len(stuid) == 0:
print("输入的学号不能为空,请重新选择")
else:
sql1 = f"select stuid from stu1 where stuid={int(stuid)}"
obj.sql_select(sql1)
if obj.sql_select(sql1):
name = input("请输入新的姓名:").strip()
age = input("请输入新的年龄:").strip()
tel = input("请输入新的电话:").strip()
if len(name) == 0 or len(age) == 0 or len(tel) == 0:
print("不能输入为空")
else:
sql2 = f"update stu1 set name='{name}',age={age},tel={tel} where stuid={int(stuid)}"
try:
obj.sql_edit(sql2)
except Exception as err:
print(f"错误信息如下:{err}")
else:
print("修改成功")
else:
print("学生不存在")
if __name__ == '__main__':
while True:
print("\n欢迎使用学生管理系统\n")
print("1. 添加新学员")
print("2. 删除一个学员")
print("3. 查看学员信息")
print("4. 修改学员信息")
print("5. 退出系统")
try:
choice = int(input("请选择需要执行的操作(输入选项的数字):").strip())
except Exception as err:
print(f"错误信息如下:{err}")
else:
if choice==1:
addStu()
elif choice==2:
deleteStu()
elif choice==3:
selectStu()
elif choice==4:
editStu()
elif choice==5:
break