Du skal blot bruge json.dumps
for json-data (serialiseres til streng) som nævnt i dokumenterne
og lad psycopg2
udføre alt arbejde og parameterbinding:
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())
Ud:
[('cattle_A', {'1': 22, '4': 12}, {'2': 24, '6': 98})]