Advertisement
Guest User

Untitled

a guest
May 28th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. import psycopg2
  2.  
  3. try:
  4. # PostgreSQLへ接続
  5. conn = psycopg2.connect("dbname='データベース名' user='ログインユーザ名' host='ホスト名' password='****'")
  6. cur = conn.cursor()
  7. conn.set_isolation_level(0)
  8.  
  9. #データベースに投入するファイルの読み込み
  10. f = open('/ファイルパス/ファイル名')
  11.  
  12. #COPYの実行
  13. cur.copy_from( f , 'テーブル名', sep='\t', null='\\N',columns=('カラム名1','カラム名'))
  14. # sep='デリミッタ文字種:例ではTAB記号'
  15. # null='ヌル文字種:例ではNULL'
  16. #  columns=('')
  17.  
  18. conn.commit() #忘れずに!!
  19. f.close()
  20. print "OK"
  21.  
  22. except psycopg2.Error as e:
  23. print "NG Copy error! "
  24. print e.pgerror
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement