import pymysql
from pymysql.cursors import Cursor
class dbutil:
def __init__(self) -> None:
self.connection=pymysql.connect(host="localhost",user="root",passwd="1658626287y",db="love",charset='utf8')
self.cursor=self.connection.cursor()
def close(self):
if self.connection:
self.connection.close()
if self.cursor:
self.cursor.close()
def exedml(self,sql,*args):
try:
count=self.cursor.execute(sql,args)
self.connection.commit()
return count
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:
count=self.cursor.execute(sql,args)
return self.cursor.fetchall()
except Exception as e:
print(e)
if self.connection:
self.connection.rollback()
finally:
self.close()
if __name__ == "__main__":
dbutil1=dbutil()
sql="insert into love(name,age,home,arm) values(%s,%s,%s,%s)"
count=dbutil1.exedml(sql,7,21,13,69)
print(count)
##老师你看你下我这段代码有没有啥问题