retrieve the data from MySQL database, in dictionary format in python -


import mysqldb def network():      dict = {'cisco' : 'nexus', 'juniper' : 'juno', 'alcatel' : 'alc' }       #len(dict)     return (dict)    def db_store(variable):     con = mysqldb.connect("localhost","root","root","fs_company" )     cur = con.cursor()     key,value in variable.items():         cur.execute('''insert google (name, company) values (%s, %s)''', (key, value))         con.commit()     cur.execute("""select * google""")     variable = cur.fetchall()     #print variable    variable = network() db_store(variable) 

i have above code store data mysql database, want retrieve data database in dictionary format. need same

you missing 1 line. can convert variable dict this:

cur.execute("""select * google""") out = cur.fetchall() variable = {key:val key,val in out} print(variable) 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -