import asyncio
import aiomysql


async def main():
    try:
        print("👉 Đang tạo pool kết nối...")
        db_pool = await aiomysql.create_pool(
            host="127.0.0.1",       # MySQL chạy cùng máy với script
            port=3306,
            user="dang",            # user MySQL/phpMyAdmin
            password="dang",        # pass
            db="b52",               # tên database
            autocommit=True,
            minsize=1,
            maxsize=5,
        )
        print("✅ Tạo pool OK")

        async with db_pool.acquire() as conn:
            async with conn.cursor(aiomysql.DictCursor) as cur:
                await cur.execute("SELECT 1 AS ok")
                row = await cur.fetchone()
                print("✅ Query OK, kết quả:", row)

        db_pool.close()
        await db_pool.wait_closed()
        print("✅ Đã đóng pool, xong.")
    except Exception as e:
        print("❌ Lỗi khi kết nối / query:")
        print(repr(e))


if __name__ == "__main__":
    asyncio.run(main())
