import pymysql
class Mysqls(object):
def init(self):
# 读取配置文件
self.connect()
def connect(self):
self.connection = pymysql.connect(host="192.168.0.105", user="root", password="123456", database="allinone")
self.cursor = self.connection.cursor()
# 获取所以数据
def get_all(self, sql):
try:
self.cursor.execute(sql)
return self.cursor.fetchall()
except:
self.connection()
self.cursor.execute(sql)
return self.cursor.fetchall()
def mul_mode(self, sql, args=None):
# self.cursor.executemany("insert into user (id,name) values (%s,%s)",[(1,"aaa"),(2,"bbb"),(3,"ccc")]) 传参方式
self.cursor.execute(sql)
print(self.connection.commit())
# self.conn.commit()
def get_close(self):
self.cursor.close()
使用情况如下: