将Python字典(dict)对象转储到MySQL表的最快方法? [英] Quickest way to dump Python dictionary (dict) object to a MySQL table?

查看:126
本文介绍了将Python字典(dict)对象转储到MySQL表的最快方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个dict对象.我使用以下方法转储了数据:

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

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

每个字段的括号[]和'None'值代表NULLS,date.datetime代表日期值.

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

如何将此字典转储到MySQL表?谢谢!

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

打印数据显示如下:

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

如何将这些数据插入MySQL?

How to insert this data into MySQL?

推荐答案

假设您具有 MySQLdb (mysql-python)已安装:

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()

这篇关于将Python字典(dict)对象转储到MySQL表的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆