from typing import Mapping
import pymysql
class DBUtil :
config ={
'host':'localhost',
'user':'root',
'password':'root',
'db':'text01',
'charset':'utf8'
}
def __init__(self):
self.connection = pymysql.connect(**DBUtil.config)
self.cursor = self.connection.cursor()
def close (self):
if self.cursor:
self.cursor.close()
if self.connection:
self.connection.close()
#插入数据,更新数据,删除数据
def exedml(self,sql,*args):
try :
count = self.cursor.execute(sql,args)
self.connection.commit()
except Exception as e :
print(e)
if self.connection:
self.connection.rollback()
finally :
self.close()
def query_one (self,sql,*args):
try :
count = self.cursor.execute(sql,args)
return self.cursor.fetchone()
except Exception as e :
print(e)
if self.connection:
self.connection.rollback()
finally:
self.close()
def query_all(self,sql,*args):
try :
self.cursor.execute(sql,args)
emp = self.cursor.fetchall()
return emp
except Exception as e :
print(e)
if self.connection:
self.connection.rollback()
finally:
self.close()
if __name__ == '__mian__':
dbutil = DBUtil()
#sql = "insert into emp (empno,ename,sal) values(%s,%s,%s)"
#dbutil.exedml(sql,9999,'lili',12000)
sql = "select * from emp"
emps = dbutil.query_all(sql)
for e in emps:
print(e,end="\n")
为什么运行完啥都没有