3
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()
    for key,value in variable.items():
        cur.execute('''INSERT into google (name, company) values (%s, %s)''', (key, value))
        con.commit()
    cur.execute("""SELECT * FROM google""")
    variable = cur.fetchall()
    #print variable



variable = Network()
db_store(variable)

I have above code to store the data to mysql database, I want to retrieve the data from database in dictionary format. need help for the same

1
  • What did you try so far? A hint. When working with SQLite dont insert your values with %s. That could leed to formatting problems. Replace them by ?, then the databse does the formatting. Commented Aug 18, 2014 at 11:54

1 Answer 1

1

You are missing only one line. You can convert variable into dict like this:

cur.execute("""SELECT * FROM google""")
out = cur.fetchall()
variable = {key:val for key,val in out}
print(variable)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.