Python编程:peewee执行多条原生sql语句

在peewee模块中,如果已经配置好了mysql数据库的信息,而不想定义Model,可以直接使用execute_sql() 执行一条sql语句

如果我想执行多条呢?peewee模块没有找到类似execute_many()的方法

既然pymysql模块可以执行多条,而peewee模块又是对其的封装,那么我们按照pymysql模块的使用思路来试试

SQL:pymysql模块读写mysql数据

代码示例

from peewee import MySQLDatabase

db = MySQLDatabase(
    host = 'localhost', 
    database = 'datebase_name',
    user = 'root',
    passwd = 'xxx',
    charset = 'utf8'
)

# 获取游标
cursor = db.get_cursor()

# 执行多条
rowcount = cursor.executemany(sql, args)

# 提交事务
db.commit()

# 关闭游标
cursor.close()

# 关闭连接
db.close()

猜你喜欢

转载自blog.csdn.net/mouday/article/details/80773131