Python學員資訊管理系統mysql

2021-08-20 19:13:11 字數 4464 閱讀 7545

1. 編寫stu表資訊操作類:內有方法:構造方法實現資料庫連線;析構方法關閉資料連線;

findall( )--查詢方法 、del(id)-- 刪除方法   insert(data)--新增方法

2. 使用使用上面自定義stu表操作類,做出增,刪,查詢操作。

# -*- coding:utf-8 -*-

"""@time : 2018/6/8 16:38

@author: cai jinzhi

@file : student_manage_system-mysql.py

"""'''

import pymysql

db = pymysql.connect(host="localhost",user='root',password='fgh000561',db='cai_database',charset="utf8")

cursor = db.cursor()

#資料查詢

sql = "select * from stulist where classid = '%s'" % ('python03')

try:

cursor.execute(sql)

print("本次查詢條數:",cursor.rowcount)

alist = cursor.fetchall()

for i in alist:

print(i)

except exception as error:

print("sql執行錯誤,原因:",error)

#資料新增

data = ('python06','caio',25)

sql = "insert into stulist values('%s','%s','%d')" % data

try:

m = cursor.execute(sql)

db.commit()

print("成功操作條數:",m)

except exception as error:

db.rollback()

print("sql執行錯誤,原因:",error)

#資料刪除

sql = "delete from stulist where classid='python06'"

try:

cursor.execute(sql)

db.commit() #提交事務

print("成功刪除條數:",cursor.rowcount)

except exception as error:

db.rollback() #事務回滾

print("sql執行錯誤,原因:",error)

#關閉資料庫

db.close()

'''import pymysql

#定義stu表資訊操作類

class stu(object):

#構造方法實現資料庫連線

def __init__(self):

try:

self.db = pymysql.connect(host="localhost",user='root',password='fgh000561',db='cai_database',charset="utf8")

self.cursor = self.db.cursor()

#try:

#self.sql = "show tables"

#self.cursor.execute(self.sql)

#self.list1 = self.cursor.fetchall()

#if ('stulist',) in self.list1:

#self.sql1 = "select * from stulist"

#self.cursor.execute(self.sql1)

#self.alist = self.cursor.fetchall()

#for i in self.alist:

#print(i)

except exception as error:

self.db.rollback()

print("sql連線失敗,原因是:",error)

#定義乙個查詢學員資訊方法

def find_all(self):

try:

self.cursor.execute("select * from stulist")

self.alist =self.cursor.fetchall()

if self.cursor.rowcount=='0':

print("********** 沒有學員資訊可以輸出!**********===")

return

print("|| | | | |".format("id", "name", "***","age", "classid"))

print("-" * 40)

for j in (self.alist):

print("|| | | | |".format(j[0],j[1],j[2],j[3],j[4]))

except exception as error:

self.db.rollback()

print("sql查詢失敗,原因是:", error)

#定義乙個新增成員方法

def insert_new(self,new_stu):

try:

self.cursor.execute("insert into stulist values(null,'','','','')".format(new_stu["name"],new_stu["***"],new_stu["age"],new_stu["classid"]))

self.db.commit()

except exception as error:

self.db.rollback()

print("sql新增失敗,原因是:",error)

#定義乙個刪除成員的方法

def del_data(self,id):

try:

self.cursor.execute("delete from stulist where id={}".format(id))

self.db.commit()

except exception as error:

self.db.rollback()

print("sql刪除失敗,原因是:",error)

#定義乙個析構方法關閉資料庫

def __del__(self):

self.cursor.close()

self.db.close()

#類操作物件

stu = stu()

#程式主函式

while true:

# 輸出初始介面

print("="*12,"學員管理系統","="*14)

print(" ".format(" ","1. 檢視學員資訊","2. 新增學員資訊"))

print(" ".format(" ","3. 刪除學員資訊","4. 退出系統"))

print("="*40)

key = input("請輸入對應的選擇:")

# 根據鍵盤值,判斷並執行對應的操作

if key == "1":

print("="*12,"學員資訊瀏覽","="*14)

stu.find_all()

input("按回車鍵繼續:")

continue

elif key == "2":

print("="*12,"學員資訊新增","="*14)

new_stu={}

new_stu['name']=input("請輸入要新增的姓名:")

new_stu['***']=input("請輸入要新增的姓別:")

new_stu['age']=input("請輸入要新增的年齡:")

new_stu['classid']=input("請輸入要新增的班級號:")

stu.insert_new(new_stu)

stu.find_all()

input("按回車鍵繼續:")

continue

elif key == "3":

print("="*12,"學員資訊刪除","="*14)

stu.find_all()

id = int(input("請輸入你要刪除的資訊id號:"))

stu.del_data(id)

stu.find_all()

input("按回車鍵繼續:")

continue

elif key == "4":

print("="*12,"再見","="*14)

break

else:

print("*****=== 無效的鍵盤輸入! **********")

Python學員資訊管理系統

coding utf 8 time 2018 6 8 16 33 author cai jinzhi file student manage system.py 定義乙個用於存放學員資訊的列表變數 stulist 定義乙個學生資訊的輸出函式 def showstu stulist 學生資訊的輸出函式...

python學員資訊管理 呼叫函式實現

通過定義函式來實現學員資訊管理 python小白一枚,不對的地方請批評指正 下面展示一些內聯 片。定義函式,顯示功能列表給使用者 info def print info print 請選擇功能 print 1.新增學員 print 2.刪除學員 print 3.修改學員 print 4.顯示所有學員...

Python 學生資訊管理系統

遞迴實現 1 有五個學 坐在 起,問第五個 多少歲?答 第四個 2歲,第四個 說它 第三個 2歲,第 個 說他 10歲。請寫 個遞迴函式計算第 5個學 多少歲 def age num if num 1 return 10 return age num 1 2 print age 5 結果18 2 某...