-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseAndInsert.py
More file actions
57 lines (47 loc) · 2.37 KB
/
Copy pathparseAndInsert.py
File metadata and controls
57 lines (47 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import json
import psycopg2
def cleanStr4SQL(s):
return s.replace("'","`").replace("\n"," ")
def int2BoolStr (value):
if value == 0:
return 'False'
else:
return 'True'
def insert2BusinessTable():
#reading the JSON file
with open('./business.JSON','r') as f: #TODO: update path for the input file
#outfile = open('./yelp_business.SQL', 'w') #uncomment this line if you are writing the INSERT statements to an output file.
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 cussent business
# TODO: The below INSERT statement is based on a simple (and incomplete) businesstable schema. Update the statement based on your own table schema and
# include values for all businessTable attributes
sql_str = "INSERT INTO businessTable (business_id, name, address,state,city,zipcode,latitude,longitude,stars,reviewcount,numCheckins,openStatus) " \
"VALUES ('" + cleanStr4SQL(data['business_id']) + "','" + cleanStr4SQL(data["name"]) + "','" + cleanStr4SQL(data["address"]) + "','" + \
cleanStr4SQL(data["state"]) + "','" + cleanStr4SQL(data["city"]) + "','" + cleanStr4SQL(data["postal_code"]) + "'," + str(data["latitude"]) + "," + \
str(data["longitude"]) + "," + str(data["stars"]) + "," + str(data["review_count"]) + ",0 ," + \
int2BoolStr(data["is_open"]) + ");"
try:
cur.execute(sql_str)
except:
print("Insert to businessTABLE failed!")
conn.commit()
# optionally you might write the INSERT statement to a file.
# outfile.write(sql_str)
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()
insert2BusinessTable()