Skip to content

Instantly share code, notes, and snippets.

@ks-t0

ks-t0/csv2pos.py Secret

Created December 21, 2021 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ks-t0/c9047f6109921464d3878e84bd58b202 to your computer and use it in GitHub Desktop.
Save ks-t0/c9047f6109921464d3878e84bd58b202 to your computer and use it in GitHub Desktop.
import subprocess
HOST_NAME = "yyy.yyy.yyy.yyy"
DB_NAME = "postgres"
USER = "postgres"
INSERT_SQL = "/home/user/sql/insert.sql"
CSV_FILE = "data.csv"
try:
# CSVファイル取り込みsql実行
completed_process = subprocess.run(["psql", "-h", HOST_NAME, "-U", USER, "-d", DB_NAME, "-f", INSERT_SQL, "-v", "file_name=" + CSV_FILE], stdout=subprocess.PIPE)
tmp = str(completed_process.stdout.splitlines())
result = tmp.split(',')
# copyの件数
#result[1] = result[1].replace('\'','').replace('b','')
# insertの件数
result[2] = result[2].replace('\'','').replace('b','').replace(']','')
toroku_kensu = result[2].split()
print(f'登録件数:{toroku_kensu[2]}')
except Exception as e:
print(f'エラー要因:{str(e)}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment