目錄
1、項目所需模塊
2、學(xué)生管理系統(tǒng)的主要功能
3、首先創(chuàng)建數(shù)據(jù)庫
4、防止數(shù)據(jù)庫斷開操作
5、菜單
6、編寫主函數(shù)
7、添加學(xué)生信息
8、刪除學(xué)生信息
9、修改學(xué)生信息
10、查看學(xué)生信息
11、展示所有學(xué)生信息
12、最后就是調(diào)用主函數(shù)
1、項目所需模塊
編寫這個學(xué)生管理系統(tǒng)需要使用pymysql這個庫去連接數(shù)據(jù)庫
2、學(xué)生管理系統(tǒng)的主要功能
這個學(xué)生管理系統(tǒng)我做的很簡陋,只做了增刪改查和展示所有學(xué)生信息5個模塊,大家可以做得更詳細一點增加其他的功能模塊,在這里僅供大家參考,希望能給剛開始學(xué)習(xí)python的同學(xué)一點參考文章來源:http://www.zghlxwxcb.cn/news/detail-488379.html
3、首先創(chuàng)建數(shù)據(jù)庫
import pymysql
#定義數(shù)據(jù)庫連接參數(shù)
host = '127.0.0.1'
port = 3306
db = 'student'
user = 'root'
password = 'root'
#創(chuàng)建連接
con = pymysql.connect(host=host,
port=port,
db=db,
user=user,
password=password)
#創(chuàng)建游標(biāo)對象
cur = con.cursor()
#編寫sql語句
sql = """
create table if not exists students(
ids varchar(20) not null primary key,
name varchar(30) not null,
age int(2),
gender varchar(10) not null,
classes varchar(200) not null
)
"""
try:
#執(zhí)行sql語句
cur.execute(sql)
print('創(chuàng)建成功')
except Exception as e:
print(e)
print('創(chuàng)表失敗')
finally:
#關(guān)閉連接
con.close()
4、防止數(shù)據(jù)庫斷開操作
#數(shù)據(jù)庫自動檢測是否斷開并自動連接
def select_db():
# 檢查連接是否斷開,如果斷開就進行重連
con.ping(reconnect=True)
cur.execute(sql)
# 使用 fetchall() 獲取查詢結(jié)果
data = cur.fetchall()
return data
5、菜單
#菜單
def menu():
print("1.增加學(xué)生信息")
print("2.刪除學(xué)生信息")
print("3.查找學(xué)生信息")
print("4.修改學(xué)生信息")
print("5.展示所有學(xué)生信息")
print("0.退出系統(tǒng)")
choose = input("請輸入你的操作:")
return choose
6、編寫主函數(shù)
#定義主函數(shù)
def main():
print("+----------------------------------------+")
print("| 歡迎進入學(xué)生管理系統(tǒng) |")
print("+----------------------------------------+")
while True:
choose = menu()
#增加學(xué)生信息
if choose == '1':
add()
#刪除學(xué)生信息
elif choose == '2':
delete()
#查找學(xué)生信息
elif choose == '3':
fund()
#改變學(xué)生信息
elif choose == '4':
amend()
#展示所有學(xué)生信息
elif choose == '5':
show()
elif choose == '0':
print("你已退出學(xué)生管理系統(tǒng)?。。?!")
#非法輸入
else:
print("輸入錯誤,請沖向輸入??!")
7、添加學(xué)生信息
#添加學(xué)生信息
def add():
select_db()
#創(chuàng)建游標(biāo)對象
cur = con.cursor(pymysql.cursors.SSCursor)
print("增加學(xué)生信息開始")
while True:
try:
ids = input("請輸入學(xué)號:")
name = input("請輸入學(xué)生姓名:")
age = input("親輸入學(xué)生年齡")
gender = input("請輸入學(xué)生性別:")
classes = input("請輸入學(xué)生班級:")
values = [ids, name, age, gender, classes]
if gender not in ("男", "女"):
print("輸入錯誤,請輸入男或女!")
# 執(zhí)行sql,將數(shù)據(jù)錄入數(shù)據(jù)庫
sql = 'insert into students(ids,name,age,gender,classes) values(%s,%s,%s,%s,%s)'
cur.execute(sql, values)
if True:
# 提交事務(wù)
con.commit()
print('添加學(xué)生信息成功')
answer = input('是否繼續(xù)添加學(xué)生信息:“yes"or"no"\n')
if answer != 'yes':
break
else:
continue
except Exception as e:
print(e)
# 數(shù)據(jù)回滾,保護數(shù)據(jù)庫
con.rollback()
print('添加學(xué)生信息失敗')
finally:
print("[增加學(xué)生信息結(jié)束]")
con.close()
8、刪除學(xué)生信息
#刪除學(xué)生信息
def delete():
select_db()
# 創(chuàng)建游標(biāo)對象
cur = con.cursor(pymysql.cursors.SSCursor)
print("刪除學(xué)生信息開始")
while True:
try:
ids = input("請輸入你要刪除的學(xué)生學(xué)號:")
sql = 'delete from students where ids=%s'
con.commit()
# 執(zhí)行sql將數(shù)據(jù)從數(shù)據(jù)庫中刪除
cur.execute(sql, ids)
if True:
# 提交事務(wù)
con.commit()
print('刪除學(xué)生信息成功')
answer = input('是否繼續(xù)刪除學(xué)生信息:“yes"or"no"')
if answer != 'yes':
break
else:
continue
except:
# 數(shù)據(jù)回滾,保護數(shù)據(jù)庫
con.rollback()
print('刪除學(xué)生信息失敗')
finally:
print("刪除學(xué)生信息結(jié)束")
con.close()
9、修改學(xué)生信息
#修改學(xué)生信息
def amend():
select_db()
# 創(chuàng)建游標(biāo)對象
cur = con.cursor(pymysql.cursors.SSCursor)
print("修改學(xué)生信息開始")
while True:
ids = input("請輸入你要修改信息學(xué)生的學(xué)號:")
if ids != '':
name = input("請輸入你要修改信息學(xué)生的姓名:")
age = input("請輸入你要修改信息學(xué)生的年齡:")
gender = input("請輸入你要修改信息學(xué)生的性別:")
classes = input("請輸入你要修改信息學(xué)生的班級:")
try:
sql = f'update students set ids={ids},name={name},age={age},gender={gender},classes={classes}'
#執(zhí)行sql,修改數(shù)據(jù)庫數(shù)據(jù)
cur.execute(sql)
con.commit()
except:
print("修改失敗")
con.rollback()
finally:
print("修改學(xué)生信息結(jié)束")
answer = input('是否繼續(xù)修改學(xué)生信息:”yes“or"no"')
if answer != 'yes':
break
else:
continue
else:
print('輸入錯誤請重新輸入')
continue
con.close()
10、查看學(xué)生信息
#查找學(xué)生信息
def fund():
select_db()
# 創(chuàng)建游標(biāo)對象
cur = con.cursor(pymysql.cursors.SSCursor)
print("查找學(xué)生信息開始")
while True:
try:
mode = input('按學(xué)號查找請輸入:1,按姓名查找請輸入:2')
if mode =='1':
ids = input("請輸入你需要查找到學(xué)生學(xué)號:")
sql = 'select from students where ids=%s'
cur.execute(sql, ids)
con.commit()
print(cur.fetchall())
answer = input('是否繼續(xù)查找學(xué)生信息:”yes“or”no“')
if answer != 'yes':
break
else:
continue
elif mode == '2':
name = input("請輸入你需要查找的學(xué)生姓名:")
sql = 'select from students where name=%s'
cur.execute(sql, name)
con.commit()
print(cur.fetchall())
answer = input('是否繼續(xù)查找學(xué)生信息:”yes“or”no“')
if answer != 'yes':
break
else:
continue
else:
print("你的輸入有誤,請重新輸入")
continue
except:
# 數(shù)據(jù)回滾,保護數(shù)據(jù)庫
con.rollback()
finally:
print("查詢學(xué)生信息結(jié)束")
con.close()
11、展示所有學(xué)生信息
def show():
select_db()
# 創(chuàng)建游標(biāo)對象
cur = con.cursor(pymysql.cursors.SSCursor)
sql = f'select * from students'
cur.execute(sql)
all = cur.fetchall()
# print(len(all), type(all))
format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}'
print(format_title.format('ID', '姓名', '性別', '年齡'))
format_data = '{:^6}\t{:^12}\t{:^8}\t{:^10}'
for record in all:
print(format_data.format(record[0], record[1], record[2], record[3]))
con.close()
12、最后就是調(diào)用主函數(shù)
if __name__ == '__main__':
main()
我把所有代碼都放在這里了,里面也都有詳細注釋,大家應(yīng)該都能看懂,希望我的這篇發(fā)文能夠幫助那些還在學(xué)習(xí)python的同學(xué),這只是一個很基礎(chǔ)的小項目,希望大家能都自己都能跑一下代碼感受一下,今天的分享就到此為止了,謝謝大家。文章來源地址http://www.zghlxwxcb.cn/news/detail-488379.html
到了這里,關(guān)于python+mysql的學(xué)生管理系統(tǒng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!