Jednoduše použijte json.dumps
pro data json (serializuje se na řetězec), jak je uvedeno v dokumentech
a nechte psycopg2
provést veškerou práci a vazbu parametrů:
cattle_id = 'cattle_A'
step_count_dict = json.dumps({1: 22, 4: 12})
speed_dict = json.dumps({2: 24, 6: 98})
cur = con.cursor()
query = "INSERT INTO global_records(cattle_id, step_count, speed) VALUES (%s, %s, %s)"
cur.execute(query, (cattle_id, step_count_dict, speed_dict))
con.commit()
cur.execute('Select * from global_records')
print(cur.fetchall())
Ven:
[('cattle_A', {'1': 22, '4': 12}, {'2': 24, '6': 98})]