pymysql执行sql语句无效问题

  • 原因:查看pymysql中调用connect方法返回初始化连接默认非自动提交;
  • 解决:在mysql连接初始化连接时加上关键字参数赋值:autocommit=True可解决执行sql语句无效问题;

`
class MySQL:
def init(self, config: dict):
self.config = config

@property
def db(self):
    return pymysql.connect(
        host=self.config.get("host"),
        port=int(self.config.get("port")),
        database=self.config.get("database"),
        user=self.config.get("user"),
        password=self.config.get("password"),
        charset='utf8',
        autocommit=True,
    )

@property
def cursor(self):
    return self.db.cursor()

def sql(self, sql):
    return self.cursor.execute(sql)

def yml(self, yml):
    if Path.exists(yml):
        yml = Yml(yml).parameters
    for name, value in yml.items():
        self.sql(sql=value)
    self.cursor.close()
    self.db.close()`
原文地址:https://www.cnblogs.com/aquichita/p/13710634.html