Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. import pymysql.cursors
  2. import json
  3.  
  4. # tables: check_content_storage, gd_item
  5.  
  6. # Connect to the database
  7. connection = pymysql.connect(host='localhost',
  8.                              user='root',
  9.                              password='makap_91',
  10.                              db='recomend',
  11.                              charset='utf8',
  12.                              use_unicode=True,
  13.                              cursorclass=pymysql.cursors.DictCursor)
  14.  
  15. try:
  16.     with connection.cursor() as cursor:
  17.  
  18.         cursor.execute("SET NAMES utf8;") #or utf8 or any other charset you want to handle
  19.         cursor.execute("SET CHARACTER SET utf8;") #same as above
  20.         cursor.execute("SET character_set_connection=Latin1;") #same as above
  21.  
  22.         sql = "SELECT * FROM gd_item LIMIT 1;"
  23.         cursor.execute(sql)
  24.         result = cursor.fetchall()
  25.        
  26.         print(result)
  27.         print(result[0]['name'])
  28.         print(result[0]['name'].encode('utf-8'))
  29.        
  30.         parsed = json.loads(result)
  31.  
  32. finally:
  33.     connection.close()
  34.  
  35. if __name__ == "__main__":
  36.     with open("file.txt", 'w') as f:
  37.         f.write(json.dumps(parsed), sort_keys=True, indent=4, separators=(',', ': '))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement