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
Post a Comment