COPY FROM
在上午的大部分时间里,我一直在试图弄清楚如何格式化SQL 语句,我需要帮助。
我正在尝试将 ASCII 文本文件中的数据导入我的 Postgres 数据库中的表。我认为它不喜欢我指定输入 ASCII 文件的方式。我尝试了两个文件路径都没有运气:
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'
这是我用来访问数据库的脚本:
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"
当我在 python 控制台中单步执行脚本时,尝试运行该cur.execute(sql)
行时出现以下错误:
---------------------------------------------------------------------------
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\...
^
我是否正确地将我的字符串替换为我的 SQL 语句?