0
#!/usr/bin/python  
print('Content-type: text/html\r\n\r') 
import mysql.connector
import json

conn = mysql.connector.connect(
     user='root',
     password='',
     host='localhost',
     database='id1914180_myproducts')
     cur = conn.cursor()

     query = ("SELECT * FROM Reviews")

     cur.execute(query)
     result=cur.fetchall()
     print(json.dumps(result))

     cur.close()
     conn.close()

I want the data to be displayed in dictionary format like given:

{'UserName':'Rozi','You Me and Dupree':2.5,'Lady in the water':4.0}

Rozi khan
  • 51
  • 1
  • 2
  • Do you mean you want to print them in a pretty way ? Can you post your output from using this code ? – KALALEX Jun 25 '18 at 14:11
  • The output is in this format. Attribute names are important to me and it is not displaying that. [["Rozi", 3.5, 0.0, 4.5, 2.0, 0.0, 3.5, 0.0, 0.0], ["Ahmed", 0.0, 4.0, 3.5, 2.0, 0.0, 4.5, 0.0, 0.0], ["Amir", 5.0, 3.5, 0.0, 2.5, 0.0, 0.0, 3.0, 0.0]] @KALALEX – Rozi khan Jun 25 '18 at 14:17
  • When you trerieve a query (with `cur.fetchall()`) you get the resulting table data not the column names. You need to parse them separately, you can access them from `cur.description` [source](https://stackoverflow.com/questions/5010042/mysql-get-column-name-or-alias-from-query) – KALALEX Jun 26 '18 at 08:47
  • I get table data along column names in Python Notebook with same code but it causes this problem when i run the script from my hosting. – Rozi khan Jun 27 '18 at 08:44

0 Answers0