python - Python 数据库插件 Sqlalchemy 查询问题 ?

 

问题描述:

  • 必须要指定字段名么?不能类似 PHP 那样写了 SQL 直接用么
from sqlalchemy import create_engine

eng = create_engine("mysql+pymysql://账号:密码@地址/库")  #

with eng.connect() as con:  # 连接数据库
    result = con.execute("select * from china")  # 执行SQL返回结果
    for r in result:  # 遍历结果
        print(r)

 

第 1 个答案:

上面的代码是百度上的老代码了,新版代码是:

from sqlalchemy import text, create_engine

engine = create_engine("mysql+pymysql://账号:密码@地址/库")

with engine.connect() as connection:
    result = connection.execute(text("select username from users"))
    for row in result:
        print("username:", row.username)

这种图表哪里有案例