import pymysql
class Dbutil():
def __init__(self):
self.a=pymysql.connect(host='localhost',user='root',password='root',db='testshiwu',charset='utf8')
self.cursor=self.a.cursor()
#关闭方法
def close(self):
if self.cursor:
self.cursor.close()
if self.a:
self.a.close()
#插入数据方法
def exedml(self,sql,*args):
try:
count=self.cursor.execute(sql,args)
self.a.commit()
return count
except Exception as e:
print(e)
if self.a:
self.a.rollback()
#查看一条数据方法
def one(self,sql,*args):
try:
self.cursor.execute(sql,args)
return self.cursor.fetchone()
except Exception as e:
print(e)
finally:
self.close()
#查看全部数据方法
def all(self,sql,*args):
try:
self.cursor.execute(sql,args)
return self.cursor.fetchall()
except Exception as e:
print(e)
finally:
self.close()
if __name__=='__main__':
dutile=Dbutil()
#插入数据
# sql='insert into account (username,balance) values(%s,%s)'
# cont=dutile.exedml(sql,'小处女',80000)
# print(cont)
#查看一条数据
# sql='select * from account where balance=%s'
# co=dutile.one(sql,90000)
# print(co)
#查看全部数据
sql='select * from account'
c=dutile.all(sql)
for i in c:
print(i,end='n')



