views:

248

answers:

1

I have a dict object. I dumped the data using this:

for alldata in data: # print all data to screen
    print data[alldata]

Each field had brackets [] and 'None' values for NULLS and date.datetime for date values.

How do I dump this dict to MySQL table? Thank you!

print data displays something like this :

{'1': ['1', 'K', abc, 'xyz', None, None, None], '2': ['2', 'K', efg, 'xyz', None, None, None], '3': ['3', 'K', ijk, 'xyz', None, None, None]}

How to insert this data into MySQL?

+2  A: 

Assuming you have MySQLdb (mysql-python) installed:

sql = "INSERT INTO mytable (a,b,c) VALUES (%(qwe)s, %(asd)s, %(zxc)s);"
data = {'qwe':1, 'asd':2, 'zxc':None}

conn = MySQLdb.connect(**params)

cursor = conn.cursor()
cursor.execute(sql, data)
cursor.close()

conn.close()
newtover
print data displays something like this :{'1': ['1', 'K', abc, 'xyz', None, None, None], '2': ['2', 'K', efg, 'xyz', None, None, None], '3': ['3', 'K', ijk, 'xyz', None, None, None]}How to insert this data into MySQL?
ThinkCode
sql = "INSERT INTO mytable (a,b,c,d,e,f,g) VALUES (%s,%s,%s,%s,%s,%s,%s);"...cursor.executemany(sql, list(data))...
newtover