Python 数据库插件 Sqlalchemy 查询问题 ?
- 必须要指定字段名么?不能类似
PHP
那样写了SQL
直接用么
from sqlalchemy import create_engineeng = create_engine("mysql+pymysql://账号:密码@地址/库") #
with eng.connect() as con: # 连接数据库
result = con.execute("select * from china") # 执行SQL返回结果
for r in result: # 遍历结果
print(r)
回答:
上面的代码是百度上的老代码了,新版代码是:
python">from sqlalchemy import text, create_engineengine = 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)
- 官方文档:https://docs.sqlalchemy.org/en/20/core/connections.html
以上是 Python 数据库插件 Sqlalchemy 查询问题 ? 的全部内容, 来源链接: utcz.com/p/938933.html