Advertisement
Guest User

insert

a guest
Apr 12th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. import psycopg2
  2. import json
  3.  
  4. try:
  5.     connection = psycopg2.connect(user = "blue",
  6.                                   password = "blue",
  7.                                   host = "127.0.0.1",
  8.                                   port = "5432",
  9.                                   database = "db")
  10.     cursor = connection.cursor()
  11.     cursor.execute("GRANT ALL ON ALL TABLES IN SCHEMA public TO blue;")
  12.  
  13.     with open("raw_data.json",encoding="utf8") as json_data:
  14.         li=json.load(json_data)
  15.        
  16.     #print(type(x))== list
  17.         x=0
  18.  
  19.         for l in li:
  20.                #type(l)== dictionary    
  21.             points=l["points"]
  22.             title=l["title"]
  23.             description=l["description"]
  24.             tester_name=l["taster_name"]
  25.             taster_twitter_handle=l["taster_twitter_handle"]
  26.             price=l["price"]
  27.             designation=l["designation"]
  28.             variety=l["variety"]
  29.             region_1=l["region_1"]
  30.             region_2=l["region_2"]
  31.             province=l["province"]
  32.             country=l["country"]
  33.             winery=l["winery"]        
  34.             sql = """INSERT INTO REVIEWS
  35.                                  (          
  36.                                    POINTS ,
  37.                                    TITLE,
  38.                                    DESCRIPTION ,
  39.                                    TESTER_NAME ,
  40.                                    TESTER_TWITTER_HANDLE ,
  41.                                    PRICE ,
  42.                                    DESIGNATION ,
  43.                                    VARIETY ,
  44.                                    region_1 ,
  45.                                    region_2 ,
  46.                                    province ,
  47.                                    country ,
  48.                                    winery
  49.                                  )
  50.  
  51.                            values(      
  52.                                   {},{},{},{},{},{},{},{},{},{},{},{},{}
  53.                                  );
  54.                      """.format(points,title,description,tester_name,taster_twitter_handle,price,designation, variety,region_1,region_2,province,country,winery)
  55.            
  56.            
  57.            # print(sql)      
  58.             cursor.execute(sql)
  59.             connection.commit()
  60.             print(" json file to table insertion done.   ")
  61.    
  62. except (Exception, psycopg2.Error) as error :
  63.     print ("Error while connecting to PostgreSQL", error)
  64. finally:
  65.     #closing database connection.
  66.         if(connection):
  67.             cursor.close()
  68.             connection.close()
  69.             print("PostgreSQL connection is closed")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement