数据库
首页 > 数据库> > sqlalchemy 执行原生sql语句

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()

 

标签:语句,fetchall,execute,sqlalchemy,cursor,session,result,sql,conn
来源: https://www.cnblogs.com/wt7018/p/11617878.html