0
我一直在试图弄清楚如何在早上的大部分时间格式化一个COPY FROM
SQL语句,我需要帮助。PostgreSQL:从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'
这里是我的脚本,我使用访问我的数据库:当我通过我的脚本在Python控制台步骤
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"
,我得到以下错误,当我尝试和运行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语句?
当我尝试使用文件路径方法实现参数化查询时,出现以下错误:'psycopg2.OperationalError:无法打开文件“C:\ Users \ dan \ Desktop \ New_folder \ Sept_2014 \ R01761 \ R01761_tex。 asc“阅读:没有这样的文件或目录。但'os.path.isfile(file)'返回true。 – dubbbdan
@dubbbdan那么,你是否得到了同样的错误,如果你只是围绕占位符(答案中的第一个建议)引号?谢谢。 – alecxe
是的,我收到了同样的错误。 – dubbbdan