解决:sqlalchemy.exc.ObjectNotExecutableError: Not an executable object: ‘select 1‘

with app.app_context():
    with db.engine.connect() as conn:
        rs = conn.execute("select 1")
        print(rs.fetchone())

以上语句常用于python 中sql 的连接测试,但常会出现如下报错

sqlalchemy.exc.ObjectNotExecutableError: Not an executable object: 'select 1'

解决方法:from sqlalchemy import text 将代码改为如下

with app.app_context():
    with db.engine.connect() as conn:
        rs = conn.execute(text("select 1"))
        print(rs.fetchone())

你可能感兴趣的:(数据库,mysql,sql)