Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi

i am beginning in python and want to write this script to insert data from csv to postgresql table

Java
import csv
import psycopg2

conn_string="dbname='Training' user='admin' password='admin' "

conn=psycopg2.connect(conn_string)

cursor=conn.cursor()
print "Connected!\n"
# reader=csv.reader(open('product.product_.csv','rb'))

 
#reader=csv.reader(open(C:\Users\Mostafa\Downloads\New folder\product.product_.csv))
with open('C:\Users\Mostafa\Downloads\New folder\product.product_.csv', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        print row
    Query="insert into product_template (name) VALUES ('"+str( row )+"') RETURNED id"
    cursor.excute(Query)
    conn.commit()
print "Done!\n"


get this error
VB
Traceback (most recent call last):
  File "D:\Code\Java\Import\src\CSV\__init__.py", line 19, in <module>
    cursor.excute(Query)
AttributeError: 'psycopg2._psycopg.cursor' object has no attribute 'excute'


What can i do to solve this error.
Posted

1 solution

Probably the typo is the reason. It should be execute instead of excute. Try like below
Python
cursor.execute(Query)

Always check the sample code/syntax to save your time.
 
Share this answer
 
Comments
Mostafa Mohamed 9-May-14 12:51pm    
@thatraja thanks for replay

i solve this error but i get another

cursor.execute(statement)
psycopg2.ProgrammingError: syntax error at or near "name"
LINE 1: INSERT INTO Product (Product name) VALUES (['Service'])

when i try to run this

statement = 'INSERT INTO Product (Product name) VALUES (' + str(row) + ')'
cursor.execute(statement)
thatraja 9-May-14 13:24pm    
You missed comma between columns Product & name.
statement = 'INSERT INTO Product (Product, name) VALUES (' + str(row) + ')'
thatraja 9-May-14 13:25pm    
I suggest you spend time with SQL basics. It could save your time.
Mostafa Mohamed 9-May-14 13:30pm    
@thatraja <br>
 <br>
i missed(product_name) is column name <br>
now that can i do to run script

the same error is appear when i try to run
thatraja 9-May-14 13:32pm    
Oh OK. I thought there were 2 columns. Anyway glad you solved the issue.
And don't forget about spend more time on SQL basics

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900