該程式主要用到sqlite3
#資料庫sqlite
import sqlite3
#存放學生資訊
#student = list()
#展示選單
def showMenu():
print("1.增加學生資訊")
print("2.洗掉學生資訊")
print("3.修改學生資訊")
print("4.顯示學生資訊")
print("0.退出系統")
select = eval(input("操作:"))
return select
#添加學生資訊
def addStudent():
print("-----增加學生資訊-----")
name = input("姓名:")
sex = input("性別:")
age = input("年齡:")
phone = input("電話:")
conn = sqlite3.connect("student.db")
cur = conn.cursor()
cur.execute("insert into info (id,name,sex,age,phone)values (null,?,?,?,?)",(name,sex,age,phone))
conn.commit()
cur.close()
conn.close()
print("添加成功!")
showStudent()
#展示學生資訊
def showStudent():
#連接資料庫,進行查詢操作
conn = sqlite3.connect("student.db")
cur = conn.cursor()
cur.execute("select * from info")
data = cur.fetchall()
if len(data) > 0 :
print("-----------學生資訊------------")
print("序號\t\t姓名\t\t性別\t\t年齡\t\t電話")
for i in range(len(data)):
print(data[i][0],'\t\t',data[i][1],'\t',data[i][2],'\t',data[i][3],'\t',data[i][4])
print("------------------------------")
else:
print("----------資訊表為空-----------")
cur.close()
conn.close()
#洗掉學生資訊
def delStudent():
print("---正在進行洗掉操作---")
print("-----當前學生資訊------")
showStudent()
select = eval(input("請輸入要洗掉的學生序號:"))
#連接資料庫,進行洗掉操作
conn = sqlite3.connect("student.db")
cur = conn.cursor()
cur.execute("delete from info where id = ?",(str(select)))
conn.commit()
cur.close()
conn.close()
print("洗掉成功!")
showStudent()
#修改學生資訊
def reviseStudent():
print("-----正在進行修改操作-----")
showStudent()
num = eval(input("請輸入要修改的學生序號:"))
print("1-修改姓名\n2-修改性別\n3-修改年齡\n4-修改電話")
revisenum = eval(input("請輸入要修改的資訊序號:"))
newstr = input("請輸入新的資訊:")
#連接資料庫進行更新操作
conn = sqlite3.connect("student.db")
cur = conn.cursor()
if revisenum == 1:
cur.execute("update info set name = ? where id = ?",(str(newstr),str(num)))
conn.commit()
cur.close()
conn.close()
print("修改成功!")
showStudent()
elif revisenum == 2:
cur.execute("update info set sex = ? where id = ?", (str(newstr), str(num)))
conn.commit()
cur.close()
conn.close()
print("修改成功!")
showStudent()
elif revisenum == 3:
cur.execute("update info set age = ? where id = ?", (str(newstr), str(num)))
conn.commit()
cur.close()
conn.close()
print("修改成功!")
showStudent()
elif revisenum == 4:
cur.execute("update info set phone = ? where id = ?", (str(newstr), str(num)))
conn.commit()
cur.close()
conn.close()
print("修改成功!")
showStudent()
else:
#如果revisenum輸入有誤,就修改失敗
print("修改失敗!請輸入正確的修改資訊!")
#主要運行函式
def main():
while True:
#連接資料庫,如果資料庫不存在,默認在當前路徑下創建
conn = sqlite3.connect("student.db")
#獲取游標
cur = conn.cursor()
#創建表
cur.execute("""
create table if not exists info(
id integer primary key autoincrement,
name text(20),
sex text(20),
age text(20),
phone text(20)
)
""")
#提交事物
conn.commit()
#關閉游標
cur.close()
#關閉連接
conn.close()
#顯示選單
select = showMenu()
if select == 1:
addStudent()
elif select == 2:
delStudent()
elif select == 3:
reviseStudent()
elif select == 4:
showStudent()
elif select == 0:
#退出系統
break
else:
print("輸入有誤!請重新操作!")
continue
if __name__ == '__main__':
main()
增加學生資訊

洗掉學生資訊

修改學生資訊

顯示學生資訊

運行后會得到一個student.db檔案

安裝PyCharm插件Database Navigator可以查看student資料庫


原創來之不易,點贊收藏支持一下卑微的博主吧~~
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/233164.html
標籤:其他
下一篇:使用Vue仿一個網易云網站
