python字典添加列表,将Python字典/列表插入到SQL数据库中最有效的方法是什么?...

Imagine you have a key-value Python dictionary (or list) with larger amounts of items. Let’s say you’re reading a bigger JSON file and you’d like to store it’s contents into MySQL table with keys as names of columns and values as values itself.

JSON Example:

"display_location": {

"city":"Bratislava",

"state_name":"Slovakia",

"country_iso3166":"SK",

"latitude":"48.20000076",

"longitude":"17.20000076",

}

Then it is quite inefficient to write SQL insert like this manually:

INSERT INTO TABLE (city, state_name, country_iso3166, latitude, longitude) VALUES('%s','%s','%s','%s','%s')

% (Bratislava, Slovakia, SK, 48.20000076, 17.20000076);

(Well, it's ok with five values, but imagine there are for example five hundreds of them.)

Is there any Python class / method for effective and short-stringed SQL insert? I wrote this piece of code:

for key,value in list.iteritems():

value_type = type(value)

if value_type is unicode:

vars_to_sql.append(value.encode('ascii', 'ignore'))

keys_to_sql.append(key.encode('ascii', 'ignore'))

else:

vars_to_sql.append(value)

keys_to_sql.append(key)

keys_to_sql = ', '.join(keys_to_sql)

After that the insert looks much more simple:

INSERT INTO conditions_bratislava(%s) VALUES %r" % (keys_to_sql, tuple(vars_to_sql),)

There can be thousands of values and you will still be fine with this one insert statement.

Note that condition which will decode Unicode strings, so you won’t have “u” letters before the each value.

So, is there any more effective and prepared class or method how to insert many values in the same simple approach with short INSERT string?

解决方案

If your data is structured like that, it would lend itself more towards a document orientated database (Mongo/Couch etc...)

You can get away with something like this... I think using repr is being a little too clever...

insert_sql = 'INSERT INTO conditions_bratislava(%s) values(%s)'

cols = ', '.join(somedict)

vals = ', '.join('?' * len(somedict)) # or whatever qparam is required

to_execute = insert_sql % (cols, vals)

some_cursor.execute(to_execute, somedict.values())

On a side note:

value_type = type(value)

if value_type is unicode:

Should be written as:

if isinstance(value, unicode):

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值