Python Forum
Error when Inserting CSV file values into a postgreSQL using psycopg2
Thread Rating:
  • 3 Vote(s) - 3.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error when Inserting CSV file values into a postgreSQL using psycopg2
#3
(Oct-04-2016, 03:05 PM)nilamo Wrote:
(Oct-04-2016, 12:14 PM)klllmmm Wrote:
cur= conn.cursor()

for i, row in enumerate(reader):
    # ...
    conn.commit()
    cur.close()
You close the cursor inside the for loop.  The second time through the loop, the cursor isn't available anymore.  That might not actually be your issue (the error looks like the Vehicle ID isn't available or something), but that is definitely AN issue.

I know you can have multi-insert statements in sql server, mysql, and sqlite, but I don't know about oracle.  That might be an easier way to do it (and also faster, since there'd only be a single insert/commit instead of one for each row).  That would end up looking like this:
insert into Table_name (column1, column2, column3) values
(value1, value2, value3),
(another_value, another_value2, another_value3),
(once_more, unto_the, breach)
But all that said, the error you get doesn't really match what it looks like your code is doing, based off of your csv.  Are there any rows in the file that don't have a vehicleno?  You print each row in the for loop, at which point in the csv file, specifically, are you getting the error?

@nilamo Thank you very much, As u point out i should take out conn.commit & cur.close from the for loop. 
Also for loop has to be change as :
if i >= 0: continue
continue So now all the lines CSV file printed no error pops up, but it seems nothing is inserted into PostgreSQL data table, as nothing is printed when i run a select command.
Reply


Messages In This Thread
RE: Error when Inserting CSV file values into a postgreSQL using psycopg2 - by klllmmm - Oct-04-2016, 05:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Connect to PostgreSQL Through Jump Server and SSH Tunnel using Python? nishans 1 1,319 Jan-02-2024, 10:37 AM
Last Post: khanzain
  Trying to use 2 values from excel in my script but getting error.. cubangt 3 1,776 May-11-2022, 07:12 AM
Last Post: normanwolf
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,858 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Overwrite values in XML file with values from another XML file Paqqno 5 3,572 Apr-01-2022, 11:33 PM
Last Post: Larz60+
  How to split file by same values from column from imported CSV file? Paqqno 5 3,002 Mar-24-2022, 05:25 PM
Last Post: Paqqno
  How psycopg2 autocommit works ? johntay 3 10,994 Oct-08-2021, 11:22 AM
Last Post: Larz60+
  Psycopg2 doesn't work with python2 MedianykEugene 3 3,102 Aug-10-2021, 07:00 AM
Last Post: ndc85430
  Problems with inserting images into an Excel File FightingFarmer 2 3,523 May-12-2021, 10:03 PM
Last Post: FightingFarmer
  Printing x values from an csv file hobbyist 7 4,209 Mar-10-2021, 02:00 PM
Last Post: hobbyist
  Argparse error when inputting values tqader 2 3,002 Sep-11-2020, 07:42 PM
Last Post: buran

Forum Jump:

User Panel Messages

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