PythonでPsycopg2を使ってPostgreSQLを読書き

出典:接続、クエリ
https://qiita.com/arupaka__ri/items/5d4711c279d111622117
https://www.learning-nao.com/?p=3002

出典:withやcloseについて
https://qiita.com/fauntleroy/items/70c1bfb450bcc61c0964

出典:INSERT(1行、CSVから一括)
https://zenn.dev/u748756h/articles/2bda21dda12f3c

 

#■読み出しサンプルコード

#ライブラリ

import psycopg2

 

# データベースに接続

connection = psycopg2.connect(host='*.*.*.*',

                             user='postgres',

                             password='******',

                             database='******')

with connection:

    with connection.cursor() as cursor:

        # データ読み込み

        sql = "SELECT * FROM *****"

        cursor.execute(sql)

        result = cursor.fetchall()

        print(result)

cursor.close()

connection.close()

 

#Postgresqlでバルクインサートする方法

 

 

 

 

CSVのやり取りでは次のようにBOMに注意!

https://leber.zendesk.com/hc/ja/articles/900006770543-%E4%BD%9C%E6%88%90%E3%81%97%E3%81%9F%E3%82%A8%E3%82%AF%E3%82%BB%E3%83%AB%E3%83%87%E3%83%BC%E3%82%BF%E3%82%92CSV-UTF-8-%E3%83%87%E3%83%BC%E3%82%BF%E3%81%A7%E4%BF%9D%E5%AD%98%E3%81%99%E3%82%8B