Python Forum

Full Version: psycopg2 insert error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey im new to python and i have this error using python3 and psycopg2:
data gives :
{'sensor_id': ['sensor3', 'sensor4'], 'end_ts': 2345, 'type_event': ['three', 'four'], 'id_event': 2, 'event_ids': [1, 2, 6, 8], 'start_ts': 4456, 'polygon': [[1, 5], [2, 9]]}
of type dict and i have this code for inserting data to database :
                data = json.loads(request.data.decode())
                array = list()
                row1= []
                for row1 in data:
                        data['id_event'] = row1[0]
                        data['sensor_id'] = row1[1]
                        data['type_event'] = row1[2]
                        data['start_ts'] = row1[3]
                        data['end_ts'] = row1[4]
                        data['polygon'] = row1[5]
                postgreSQL_select_Query = "insert into event_table (Id_event, sensor_id, type_event, start_ts, end_ts, polygon) values (%s,%s,%s,%s,%s,%s)"
                cursor.execute(postgreSQL_select_Query, [row1])
and i get all data from json :
{ "id_event": 2,
  "sensor_id": [ "sensor3", "sensor4"],
  "type_event": ["three", "four"],
  "start_ts": 4456,
  "end_ts": 2345,
  "polygon": [[1,5], [2,9]],
  "event_ids": [1, 2, 6, 8]
}
when i run it using POST i get this error :
cursor.execute(postgreSQL_select_Query, [row1])
IndexError: list index out of range
what am i doing wrong ?