Created
March 5, 2017 02:47
-
-
Save eliocapelati/4f7e51f6e4a80fd067b5eb8d5860e6b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import psycopg2 | |
from pprint import pprint | |
def main(): | |
sql_insert = """INSERT INTO | |
worldbank_countries(countries) | |
VALUES (%s);""" | |
with open('world_bank_countries.json') as data_file: | |
data = json.load(data_file) | |
conn = None | |
cur = None | |
try: | |
conn = psycopg2.connect(host="localhost", database="postgres", user="postgres", password="myincridiblepasswd") | |
cur = conn.cursor() | |
for item in data: | |
cur.execute(sql_insert, (json.dumps(item),)) | |
except (Exception, psycopg2.DatabaseError) as error: | |
print(error) | |
finally: | |
if conn is not None: | |
cur.close() | |
conn.commit() | |
conn.close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment