asyncpg icon indicating copy to clipboard operation
asyncpg copied to clipboard

prepared statement get_attributes

Open fbeqirllari opened this issue 2 years ago • 1 comments

How can i get scale and precision from get_attributes()?

psycopg returns (Column(name='column_name', type_code=1043, display_size=None, internal_size=-1, precision=None, scale=None, null_ok=None)

asyncpg returns Attribute( name='typname', type=Type(oid=19, name='name', kind='scalar', schema='pg_catalog')

fbeqirllari avatar May 12 '22 11:05 fbeqirllari

import asyncpg

async def get_precision_and_scale():
    # Create a connection to your PostgreSQL database
    conn = await asyncpg.connect(database="your_database", user="your_user", password="your_password", host="your_host", port="your_port")

    # Execute a query to fetch column information
    query = "SELECT column_name FROM your_table LIMIT 1"
    result = await conn.fetch(query)

    # Get the column type
    column_type = result[0]['column_name'].__class__

    # Extract precision and scale if applicable
    precision = column_type.precision
    scale = column_type.scale

    # Close the connection when done
    await conn.close()

    print(f"Precision: {precision}, Scale: {scale}")

# Run the async function
asyncio.run(get_precision_and_scale())

ljluestc avatar Dec 16 '23 22:12 ljluestc