I have been trying to figure out how to format a COPY FROM SQL statement for most of the morning and I need help.
I am trying to import data from an ASCII text file to a table in my Postgres database. I am thinking it does not like how I am specifying the input ASCII file. I have tried both of the file paths with no luck:
file = os.path.normpath(os.path.join('c:\\','Users','dan','Desktop','New_Folder','Sept_2014','R01761','R01761_tex.asc'))
file = r'C:\Users\dan\Desktop\New_folder\Sept_2014\R01761\R01761_tex.asc'
Here is my script I am using to access my database:
import psycopg2
try:
conn = psycopg2.connect("dbname='reach_4a' user='root' host='localhost' port='9000' password='myPassword'")
tblname = "sept_2014""
file = r"C:\Users\dan\Desktop\New_folder\Sept_2014\R01761\R01761_tex.asc"
#file = os.path.normpath(os.path.join('c:\\','Users','dan','Desktop','New_Folder','Sept_2014','R01761','R01761_tex.asc'))
cur = conn.cursor()
sql = "COPY %s (easting,northing,texture) FROM %s DELIMITERS ' ';" % (tblname,file)
cur.execute(sql)
conn.commit()
except:
print "I am unable to connect to the database"
#Close Database
try:
conn.close()
print 'Database connection destroyed'
except:
print "I cant close the database"
When I step through my script in a python console, I get the following error when i try and run the cur.execute(sql) line:
---------------------------------------------------------------------------
ProgrammingError Traceback (most recent call last)
<ipython-input-34-c26e11f8fb81> in <module>()
----> 1 cur.execute(sql)
ProgrammingError: syntax error at or near "c"
LINE 1: COPY sept_2014 (easting,northing,texture) FROM c:\Users\dan\...
^
Am I properly substituting my strings to my SQL statement?