I'm trying to insert these records
test = [(30654, '2022-04-06', 2, 3, 4, 2, 100), (30655, '2022-04-06', 3, 5, 2, 1, 200)]
into a table with the definition
stock_id INTEGER NOT NULL,
dt TIMESTAMP WITHOUT TIME ZONE NOT NULL,
open NUMERIC NOT NULL,
high NUMERIC NOT NULL,
low NUMERIC NOT NULL,
close NUMERIC NOT NULL,
volume NUMERIC NOT NULL,
with asyncio.
I create a connection to my database via get_price and use write_to_db to insert the records:
async def write_to_db(connection, test):
await connection.copy_records_to_table("stock_price", records=test)
print("finished")
async def get_price(test):
# create database connection pool
pool = await asyncpg.create_pool(user=config.DB_USER, password=config.DB_PASS, database=config.DB_NAME,
host=config.DB_HOST, command_timeout=60)
try:
async with pool.acquire() as connection:
# async with aiohttp.ClientSession() as session:
# async with session.get(url=url) as response:
# resp = await response.read()
# response = json.loads(resp)
await write_to_db(connection, test)
except Exception as e:
print("Unable to write to copy records {} due to {}.".format(test, e.__class__))
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(get_price(test))
But I receive
Unable to write to copy records [(30654, '2022-04-06', 2, 3, 4, 2, 100), (30655, '2022-04-06', 3, 5, 2, 1, 200)] due to <class 'TypeError'>.
Why is that? In this stackoverflow post: Best way to insert multiple rows with asyncpg
somebody suggests inserting records via passing on tuples in a list, hence I don't understand why it would return a TypeError.