Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ProgrammingError: unterminated quoted string at or near "'om5ZiponkpRqUNa3pVPiRg,`);"
- CREATE TABLE user_profile (
- user_id VARCHAR(50) PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- funny INTEGER DEFAULT 0,
- review_count INTEGER DEFAULT 0,
- yelping_since DATE,
- average_stars FLOAT DEFAULT 0,
- cool INTEGER DEFAULT 0,
- fans INTEGER DEFAULT 0,
- useful INTEGER DEFAULT 0
- );
- CREATE TABLE friend (
- user_id VARCHAR(50),
- friend_id VARCHAR(50),
- PRIMARY KEY (user_id, friend_id),
- FOREIGN KEY (user_id) REFERENCES user_profile(user_id),
- FOREIGN KEY (friend_id) REFERENCES user_profile(user_id)
- );
- import json
- import psycopg2
- import pandas as pd
- from datetime import datetime
- from collections import OrderedDict, defaultdict
- def cleanStr4SQL(s):
- return s.replace("'","`").replace("n"," ")
- def int2BoolStr (value):
- if value == 0:
- return 'False'
- else:
- return 'True'
- def insert2UserTable():
- #reading the JSON file
- with open('yelp_user_test.JSON','r') as f:
- line = f.readline()
- count_line = 0
- #connect to yelpdb database on postgres server using psycopg2
- #TODO: update the database name, username, and password
- try:
- conn = psycopg2.connect("dbname='yelpdb' user='postgres' host='localhost' password='none'")
- except:
- print('Unable to connect to the database!')
- cur = conn.cursor()
- while line:
- data = json.loads(line)
- #Generate the INSERT statement for the current user
- sql_str = "INSERT INTO user_profile (user_id, name, funny,review_count,yelping_since,average_stars,cool, fans, useful) "
- "VALUES ('" + cleanStr4SQL(data['user_id']) + "','" + cleanStr4SQL(data["name"]) + "','" + cleanStr4SQL(str(data["funny"])) + "','" +
- cleanStr4SQL(str(data["review_count"])) + "','" + cleanStr4SQL(str(datetime.strptime(data['yelping_since'],'%Y-%m-%d'))) + "','" + cleanStr4SQL(str(data["average_stars"])) + "'," + str(data["cool"]) + "," +
- cleanStr4SQL(str(data["fans"])) + "," + cleanStr4SQL(str(data["useful"])) + ");"
- # try:
- cur.execute(sql_str)
- # except:
- # print("Insert to userTABLE failed!")
- conn.commit()
- line = f.readline()
- count_line +=1
- cur.close()
- conn.close()
- print(count_line)
- #outfile.close() #uncomment this line if you are writing the INSERT statements to an output file.
- f.close()
- def insert2FriendTable():
- #reading the JSON file
- with open('yelp_user_test.JSON','r') as f: #
- line = f.readline()
- count_line = 0
- try:
- conn = psycopg2.connect("dbname='yelpdb' user='postgres' host='localhost' password='none'")
- except:
- print('Unable to connect to the database!')
- cur = conn.cursor()
- while line:
- data = json.loads(line)
- friend_id = str([item for item in data['friends']])
- for i in range(len(friend_id)):
- sql_str = "INSERT INTO friend (user_id, friend_id) "
- "VALUES ('" + cleanStr4SQL(data['user_id']) + "," + cleanStr4SQL(friend_id[i]) + ");"
- print(i)
- cur.execute(sql_str)
- conn.commit()
- # try:
- # cur.execute(sql_str)
- # except:
- # print("Insert to userTABLE failed!")
- # conn.commit()
- line = f.readline()
- count_line +=1
- cur.close()
- conn.close()
- print(count_line)
- f.close()
- {"average_stars": 3.94, "compliment_cool": 1556, "compliment_cute": 211, "compliment_funny": 1556, "compliment_hot": 1285, "compliment_list": 101, "compliment_more": 134, "compliment_note": 1295, "compliment_photos": 162, "compliment_plain": 2134, "compliment_profile": 74, "compliment_writer": 402, "cool": 40110, "elite": [2014, 2017, 2011, 2012, 2015, 2009, 2013, 2007, 2016, 2006, 2010, 2008], "fans": 835, "friends": ["U_sn0B-HWdTSlHNXIl_4XA", "pnfVIB7UhvCQ7X2K0Q2XIw", "jVYzrVblDFSuL3GHtt8ZSA", "Z7bpqY89ZiBHXdo7UN1kiw", "8Aqr35f254lOeitNowt7ig", "zjcN27kCVeK8K2ONe9Qt4g", "8drMKNHWavs2g6uf0pLtvg", "_K2ViyfmVq6nzIitR0TIlg", "rUV1FUhji5xMjNBpcq5SXg", "yrGIgk5eaWy-eewLNv4KHQ", "3Vd_ATdvvuVVgn_YCpz8fw", "ebC_pH92K4uxyDenoXb5bg", "RJrGgtBXkpX2oEHM4hSqXg", "sdpIz4-s15T239CZ4Bd6Ag", "A0j21z2Q1HGic7jW6e9h7A", "AvC5XQAElcGAAn_Wr5auEg", "JlkHKBnHKdK8Tpls0AF5Aw", "8AG5MctcxTjP4svmUrt0yQ", "bKxdvn7KpmWjMzlmBvp-Xw", "VVMS74JyUk2h53yfC-xNsA", "-ro7OG3jjCSKnF6OJinKjg", "K7thO1n-vZ9PFYiC7nTR2w", "pRBzWnFzaCEtqhYyJ2ZTDQ", "nwESZ8e-KzXt2fKkOuRdIQ", "WNZfkL4DBspueoGSUOMAqA", "uU6fQWadr7Hx_MP0Vmy3kQ", "XiLxIJThWsE0x4d0IeSPsg", "Y9LBTbwO4g0BmdBIi0D3CA", "jGbj8fl575EIQJcfaA1FKQ", "nxWrhF_hyX0wwjrEkQX8uQ", "670k6Gr6V4VqLIKtVEmDuQ", "o5STsEtfvD1Ig0J7Z-1uxA", "x13yoEggBL0pIE7-KMnhDQ", "rCx7tb3toOJUsvdOeqYY0g", "nkN_do3fJ9xekchVC-v68A", "KzHRsFwryS7b5Fog8kkkGA", "_pBzBgtCTN9PNUPfgPDI8A", "PU5QaMADa6N_9ZoQ04ZjOw", "FkfpHzqoDRChwOYhA6NPnQ", "CaQy-zz10ajG7KkNSbXi5w", "brQ7OjB6f9nXWGk45A9A3g"], "funny": 10882, "name": "Andrea", "review_count": 2559, "useful": 83681, "user_id": "om5ZiponkpRqUNa3pVPiRg", "yelping_since": "2006-01-18"}
- user_id friend_id
- user_id friend_id
- user_id friend_id
- om5ZiponkpRqUNa3pVPiRg U_sn0B-HWdTSlHNXIl_4XA
- om5ZiponkpRqUNa3pVPiRg pnfVIB7UhvCQ7X2K0Q2XIw
Add Comment
Please, Sign In to add comment