pymysql實現的簡單學生資訊管理

2021-10-07 00:07:49 字數 2758 閱讀 3955

import pymysql.cursors

import datetime

class experiment5:

def __init__(self):

self.connect = pymysql.connect(

host='localhost',

port=3306,

user='root',

passwd='',

db='tt',

charset='utf8'

)self.cursor = self.connect.cursor()

def delete(self):

try:

try:

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

except:

print("輸入非法")

return

sql = "delete from student where sno=%d" % id

print(sql)

self.cursor.execute(sql)

self.connect.commit()

print('成功刪除', self.cursor.rowcount, '條資料')

except exception as e:

# print(str(e))

print("不存在該資料")

def add(self):

try:

sql = "insert into student (sname, ***, sage) values ('%s','%s','%s' )"

sname = input("名字:")

*** = input("性別:")

sage = input("年齡:")

data = (sname, ***, sage)

self.cursor.execute(sql % data)

self.connect.commit()

print('成功插入', self.cursor.rowcount, '條資料')

print()

except:

print("存在約束條件!無法插入!")

def update(self):

self.showall()

try:

try:

id = int(input("請輸入要修改的id序號:"))

except:

print("輸入非法")

return

sql = "update student set sname='%s',sage=%d,***='%s' where sno=%d"

sname = input("名字:")

*** = input("性別:")

try:

sage = int(input("年齡:"))

except:

print("輸入非法")

return

data = (sname, sage, ***, id)

self.cursor.execute(sql % data)

self.connect.commit()

print('成功修改', self.cursor.rowcount, '條資料')

except exception as e:

# print(str(e))

print("修改失敗!")

def showall(self):

try:

sql = "select * from student"

self.cursor.execute(sql)

self.connect.commit()

for row in self.cursor.fetchall():

print("編號:%d\t學生姓名:%s\t年齡:%d\t性別:%s" % row)

print('共查詢出', self.cursor.rowcount, '條資料')

print()

except exception as e:

# print(str(e))

print("不存在該資料!")

def query(self):

try:

query = input("查詢條件:(姓名='1');(年齡》1);(性別='男')")

query=query.replace("姓名","sname")

query=query.replace("年齡","sage")

query=query.replace("性別","***")

queries = query.split(";")

query = " and ".join(queries)

sql = "select * from student where "+query

print(sql)

self.cursor.execute(sql)

self.connect.commit()

for row in self.cursor.fetchall():

print("編號:%d\t學生姓名:%s\t年齡:%d\t性別:%s" % row)

print('共查詢出', self.cursor.rowcount, '條資料')

print()

except exception as e:

print(str(e))

print("輸入條件錯誤!")

對pymysql的簡單封裝

coding utf 8 usr bin python import pymysql class mysql 對pymysql的簡單封裝 def init self,host,user,pwd,db self.host host self.user user self.pwd pwd self.db...

pymysql的使用簡單使用方法

1.安裝方法 pip安裝 pip install pymysql anaconda安裝 conda install c anaconda pymysql 2.pymysql執行流程 3.匯入模組 from pymysql import 4.例項 import pymysql 連線資料庫 db pym...

雙鏈表實現簡單的學生管理系統

include include stdlib.h include string.h filename sm.c authour self chou version 1.0 date 2012.07 description 用鍊錶實現簡單的學生管理系統 function list create 學生資...