sqlalchemy 执行原生sql语句

1、方式一

复制代码
# 查询
cursor = session.execute('select * from users')
result = cursor.fetchall()

# 添加
cursor = session.execute('insert into users(name) values(:value)', params={"value": 'abc'})
session.commit()
print(cursor.lastrowid)
复制代码

2、方式二

复制代码
conn = engine.raw_connection()
cursor = conn.cursor()
cursor.execute(
    "select * from t1"
)
result = cursor.fetchall()
cursor.close()
conn.close()
复制代码

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注