Guest User

Untitled

a guest
Mar 15th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. ProgrammingError: unterminated quoted string at or near "'om5ZiponkpRqUNa3pVPiRg,`);"
  2.  
  3. CREATE TABLE user_profile (
  4. user_id VARCHAR(50) PRIMARY KEY,
  5. name VARCHAR(100) NOT NULL,
  6. funny INTEGER DEFAULT 0,
  7. review_count INTEGER DEFAULT 0,
  8. yelping_since DATE,
  9. average_stars FLOAT DEFAULT 0,
  10. cool INTEGER DEFAULT 0,
  11. fans INTEGER DEFAULT 0,
  12. useful INTEGER DEFAULT 0
  13. );
  14.  
  15. CREATE TABLE friend (
  16. user_id VARCHAR(50),
  17. friend_id VARCHAR(50),
  18. PRIMARY KEY (user_id, friend_id),
  19. FOREIGN KEY (user_id) REFERENCES user_profile(user_id),
  20. FOREIGN KEY (friend_id) REFERENCES user_profile(user_id)
  21.  
  22. );
  23.  
  24. import json
  25. import psycopg2
  26. import pandas as pd
  27. from datetime import datetime
  28. from collections import OrderedDict, defaultdict
  29.  
  30. def cleanStr4SQL(s):
  31. return s.replace("'","`").replace("n"," ")
  32.  
  33. def int2BoolStr (value):
  34. if value == 0:
  35. return 'False'
  36. else:
  37. return 'True'
  38. def insert2UserTable():
  39. #reading the JSON file
  40. with open('yelp_user_test.JSON','r') as f:
  41.  
  42. line = f.readline()
  43. count_line = 0
  44.  
  45. #connect to yelpdb database on postgres server using psycopg2
  46. #TODO: update the database name, username, and password
  47. try:
  48. conn = psycopg2.connect("dbname='yelpdb' user='postgres' host='localhost' password='none'")
  49. except:
  50. print('Unable to connect to the database!')
  51. cur = conn.cursor()
  52.  
  53. while line:
  54. data = json.loads(line)
  55. #Generate the INSERT statement for the current user
  56.  
  57. sql_str = "INSERT INTO user_profile (user_id, name, funny,review_count,yelping_since,average_stars,cool, fans, useful) "
  58. "VALUES ('" + cleanStr4SQL(data['user_id']) + "','" + cleanStr4SQL(data["name"]) + "','" + cleanStr4SQL(str(data["funny"])) + "','" +
  59. cleanStr4SQL(str(data["review_count"])) + "','" + cleanStr4SQL(str(datetime.strptime(data['yelping_since'],'%Y-%m-%d'))) + "','" + cleanStr4SQL(str(data["average_stars"])) + "'," + str(data["cool"]) + "," +
  60. cleanStr4SQL(str(data["fans"])) + "," + cleanStr4SQL(str(data["useful"])) + ");"
  61. # try:
  62. cur.execute(sql_str)
  63. # except:
  64. # print("Insert to userTABLE failed!")
  65. conn.commit()
  66.  
  67.  
  68. line = f.readline()
  69. count_line +=1
  70.  
  71. cur.close()
  72. conn.close()
  73.  
  74. print(count_line)
  75. #outfile.close() #uncomment this line if you are writing the INSERT statements to an output file.
  76. f.close()
  77.  
  78. def insert2FriendTable():
  79. #reading the JSON file
  80. with open('yelp_user_test.JSON','r') as f: #
  81. line = f.readline()
  82. count_line = 0
  83.  
  84.  
  85. try:
  86. conn = psycopg2.connect("dbname='yelpdb' user='postgres' host='localhost' password='none'")
  87. except:
  88. print('Unable to connect to the database!')
  89. cur = conn.cursor()
  90.  
  91. while line:
  92. data = json.loads(line)
  93.  
  94. friend_id = str([item for item in data['friends']])
  95. for i in range(len(friend_id)):
  96. sql_str = "INSERT INTO friend (user_id, friend_id) "
  97. "VALUES ('" + cleanStr4SQL(data['user_id']) + "," + cleanStr4SQL(friend_id[i]) + ");"
  98. print(i)
  99. cur.execute(sql_str)
  100. conn.commit()
  101.  
  102. # try:
  103. # cur.execute(sql_str)
  104. # except:
  105. # print("Insert to userTABLE failed!")
  106. # conn.commit()
  107.  
  108.  
  109. line = f.readline()
  110. count_line +=1
  111.  
  112. cur.close()
  113. conn.close()
  114.  
  115. print(count_line)
  116.  
  117. f.close()
  118.  
  119. {"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"}
  120.  
  121. user_id friend_id
  122.  
  123. user_id friend_id
  124.  
  125. user_id friend_id
  126.  
  127. om5ZiponkpRqUNa3pVPiRg U_sn0B-HWdTSlHNXIl_4XA
  128.  
  129. om5ZiponkpRqUNa3pVPiRg pnfVIB7UhvCQ7X2K0Q2XIw
Add Comment
Please, Sign In to add comment