鍍金池/ 問答/Python  數(shù)據(jù)庫  網(wǎng)絡(luò)安全/ 關(guān)于aiomysql配合sqlalchemy導入表結(jié)構(gòu)的問題

關(guān)于aiomysql配合sqlalchemy導入表結(jié)構(gòu)的問題

import asyncio
import sqlalchemy as sa

from aiomysql.sa import create_engine


@asyncio.coroutine
def go():
    engine = yield from create_engine(user='root',db='test',host='127.0.0.1',password='root')

    metadata = sa.MetaData(bind=engine)
    tbl = sa.Table('tbl', metadata
        ,sa.Column('id', sa.Integer, primary_key=True)
        ,sa.Column('val', sa.String(255))
    )
    with (yield from engine) as conn:
        res = yield from conn.execute(tbl.select())
        for row in res:
            print(row.id, row.val)

asyncio.get_event_loop().run_until_complete(go())


上述是aiomysql所給出的官方示例,我想問的是,對于每一張表在上述的案例中都要使用sa.Table()方法進行數(shù)據(jù)表的映射,并且在這個映射中必須要列出所有的數(shù)據(jù)列。
在單獨使用sqlalchemy(不使用aiomysql)的時候可以通過如下方法導入

alphaTable = Table('alpha', metadata, autoload=True)

但是配合上aiomysql之后會出現(xiàn)報錯,想問有沒有什么辦法是可以不用自己枚舉信息列直接通過某些方法導入?

回答
編輯回答
解夏

額外創(chuàng)建一個普通的 SQLAlchemy engine,然后用 autoload_with=blocking_engine 替代 autoload=True。因為表映射應(yīng)該只發(fā)生一次,所以此時不使用異步應(yīng)該可以接受。

2018年7月29日 21:04