0

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"

cur.execute(sql)Python コンソールでスクリプトをステップ実行すると、行を実行しようとすると次のエラーが表示されます。

---------------------------------------------------------------------------
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 ステートメントに適切に置き換えていますか?

4

1 に答える 1