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に注意!