Thi*_*ode 7 python mysql dictionary
我有一个dict对象.我使用这个转储数据:
for alldata in data: # print all data to screen
print data[alldata]
Run Code Online (Sandbox Code Playgroud)
每个字段都有NULLS的括号[]和'无'值以及日期值的date.datetime.
如何将此dict转储到MySQL表?谢谢!
打印数据显示如下:
{'1': ['1', 'K', abc, 'xyz', None, None, None], '2': ['2', 'K', efg, 'xyz', None, None, None], '3': ['3', 'K', ijk, 'xyz', None, None, None]}
Run Code Online (Sandbox Code Playgroud)
如何将这些数据插入MySQL?
new*_*ver 11
假设你安装了MySQLdb(mysql-python):
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()
Run Code Online (Sandbox Code Playgroud)