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