Need help with the error. Below is my function to insert data to postgres db
import psycopg2
def insert_records(data):
conn = psycopg2.connect(
database = "postgres",
user = "some_user",
password = "some_password",
host = "***rds.amazonaws.com",
port="5432"
)
conn.autocommit = True
cursor = conn.cursor()
column_names = ', '.join(data[0].keys()) -->output ID, FIRST_NAME, LAST_NAME
placeholders = ', '.join(['%s' for _ in range(len(data[0]))]) --->output %s, %s, %s
insert_query = f"INSERT INTO {table_name} ({column_names}) VALUES ({placeholders})"
cursor.executemany(insert_query, data)
the data being used is below:
[{'ID':'ID1234', 'FIRST_NAME':'Thomas', 'LAST_NAME':'Edison'}]
Getting the type error
cursor.executemany(insert_query, data) "TypeError: dict is not a sequence"
According to the documentation the documentation data should be a tuple sequence, not a dict (Example: {name: harry}) ]. Like so: