python 流式游標讀取mysql大型數據庫


import asyncio
import aiomysql


async def dbdaochu(loop):
    sqlstr='sql'
    conn = await aiomysql.connect(host, username,
                           pwd, db, charset='utf8',
                                  loop=loop)
    async with aiomysql.cursors.SSCursor(conn) as cursor:
        await cursor.execute(sqlstr)
        while True:
            row = await cursor.fetchone()
            if not row:
                break

            print(row)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(dbdaochu(loop))
    loop.close()
###########Pool版本
async def dbdaochu(loop):
    sqlstr='..........'
    pool = await aiomysql.create_pool(minsize=1, maxsize=10,host=host, user=user,
                           password=password,db=db, charset='utf8',
                                  loop=loop)

    async with pool.acquire() as conn:
        async with aiomysql.cursors.SSCursor(conn) as cur:
            await cur.execute(sqlstr)
            while True:
                row = await cur.fetchone()
                if not row:
                    break
                print(row)
    pool.close()
    await pool.wait_closed()

if __name__ == '__main__':

    loop = asyncio.get_event_loop()
    loop.run_until_complete(dbdaochu(loop))
    loop.close()

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM