Advertisement
Guest User

Untitled

a guest
Mar 4th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. #!/usr/bin/python
  2. # coding: utf-8
  3. import sys
  4. import psycopg2
  5.  
  6. def execute_sql_from_file(sqlfile):
  7. try:
  8. cnn = psycopg2.connect("dbname=my_db host=my_db_endpoint user=postgres password=my_password")
  9. cur = cnn.cursor()
  10.  
  11. target_sql_file = open(sqlfile)
  12. sql_data = target_sql_file.read() # ファイル終端まで全て読んだデータを返す
  13. target_sql_file.close()
  14.  
  15. cnn.commit()
  16. cur.execute(sql_data)
  17. rows = cur.fetchall()
  18.  
  19. cur.close()
  20. cnn.close()
  21.  
  22. return rows
  23. except (psycopg2.OperationalError) as e:
  24. print (e)
  25.  
  26. ## 関数実行(Main処理相当)
  27. if __name__ == "__main__":
  28. my_result = execute_sql_from_file( sys.argv[1] )
  29. print(my_result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement