关于window下python运行sqlalchemy和linux下的不同

window下的代码

from sqlalchemy import create_engine
engine = create_engine("mysql+pymysql://xxxx:xxxx@xxx:3306/xx?charset=utf8")
truncate_sql = "truncate table %s " %(table_name)
engine.execute(truncate_sql)

错误1:

engine object has no execute

错误2:

sqlalchemy.exc.ArgumentError: Textual SQL expression '' should be explicitly declared as text('')

linux修改过后的代码:

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql import text


engine = create_engine("mysql+pymysql://xx:xxxx@xxxxx:3306/xxxx?charset=utf8")
 #创建一个Session对象
 Session = sessionmaker(bind=engine)
 session = Session()
 #使用Session对象执行SQL
 session.execute(text("truncate table xxxx))

 

posted @ 2023-09-22 10:50  无敌小阿没  阅读(145)  评论(0编辑  收藏  举报