Skip to content

Latest commit

 

History

History
43 lines (28 loc) · 935 Bytes

README.md

File metadata and controls

43 lines (28 loc) · 935 Bytes

Build Status

Read this in other languages: English, 简体中文.

import asyncio
from sqlblock import AsyncPostgresSQL

conn = AsyncPostgresSQL(dsn="postgresql://postgres@localhost/test")

@conn.transaction
async def hello_world():

    await create_table()
    await init_data(start_sn=100)

    conn.sql("SELECT * FROM tmp_tbl")

    assert [r.sn async for r in conn] == [100, 101, 102, 103]

    async for r in conn:
        print(r.sn)

async def create_table():
    await conn.sql("""
    CREATE TEMPORARY TABLE tmp_tbl (
        sn INTEGER
    )
    """)

async def init_data(start_sn):
    for i in range(4):
        await conn.sql("INSERT INTO tmp_tbl (sn) VALUES ({start_sn + i}) ")

async def main():
    async with conn:
        await hello_world()

asyncio.run(main())