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)
阅读 2.3k
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)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题