python 數(shù)據(jù)庫插件 sqlalchemy 查詢的特殊之處
在 Python 中,使用 sqlalchemy 等數(shù)據(jù)庫插件進(jìn)行查詢時,需要留意其特殊之處:
字段名指定
與 php 等語言不同,sqlalchemy 查詢需要顯式指定字段名。在上例中,需要使用 con.execute(“select * from china”) 查詢所有字段,而不能像 php 那樣直接使用 sql。
新版代碼
百度上部分老代碼已過時,新版代碼如下:
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)
登錄后復(fù)制
官方文檔參考
更多詳細(xì)內(nèi)容,可參考官方文檔:https://docs.sqlalchemy.org/en/20/core/connections.html