fork download
  1. import pymysql
  2.  
  3. import os
  4. import json
  5.  
  6. # read JSON file which is in the next parent folder
  7. file = 'C://Users//risha//PycharmProjects//Test//video.json'
  8. json_data= open(file).read()
  9. json_obj = json.loads(json_data)
  10.  
  11.  
  12. # do validation and checks before insert
  13. def validate_string(val):
  14. if val != None:
  15. if type(val) is int:
  16. #for x in val:
  17. # print(x)
  18. return str(val).encode('utf-8')
  19. else:
  20. return val
  21.  
  22.  
  23. # connect to MySQL
  24. con = pymysql.connect(host = '127.0.0.1',user = 'root',passwd = '',db = 'test',port ='3306')
  25. cursor = con.cursor()
  26.  
  27.  
  28. # parse json data to SQL insert
  29. for i, item in enumerate(json_obj):
  30. person = validate_string(item.get("person", None))
  31. year = validate_string(item.get("year", None))
  32. company = validate_string(item.get("company", None))
  33.  
  34. cursor.execute("INSERT INTO testp (person, year, company) VALUES (%s, %s, %s)", (person, year, company))
  35. con.commit()
  36. con.close()
Runtime error #stdin #stdout #stderr 0.04s 9376KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 1, in <module>
ImportError: No module named 'pymysql'