Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
psycopg2 insert error
#1
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 ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  sqlite3 Conn Insert Value Error TylerDunbar 3 676 Sep-04-2023, 06:32 PM
Last Post: deanhystad
  pymysql: insert query throws error wardancer84 12 4,421 Jan-28-2022, 06:48 AM
Last Post: wardancer84
  How psycopg2 autocommit works ? johntay 3 10,210 Oct-08-2021, 11:22 AM
Last Post: Larz60+
  Psycopg2 doesn't work with python2 MedianykEugene 3 2,889 Aug-10-2021, 07:00 AM
Last Post: ndc85430
  PostgreSQL psycopg2.errors.DuplicateColumn: column specified more than once rajnish_nationfirst 2 3,697 Jun-21-2020, 08:17 AM
Last Post: ibreeden
  tornado psycopg2 Nikosznb 1 2,282 Feb-23-2020, 10:49 PM
Last Post: scidam
  Frequency and timing of psycopg2 commits acecase 0 1,956 Nov-01-2019, 05:50 PM
Last Post: acecase
  Create table with psycopg2 on postgreSQL DB yhecohen 2 3,276 Aug-23-2019, 05:56 AM
Last Post: massimo_m
  import psycopg2 issue bhuvneshdogra 1 2,957 Dec-27-2018, 04:03 PM
Last Post: Larz60+
  Need help on a psycopg2 question badfish 1 5,442 Nov-28-2018, 10:16 PM
Last Post: badfish

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020