python 字典 插入mysql,使用python字典插入到mysql中

I am trying to take the data from a dictionary (the example is simplified for readability) and insert it into a mysql database.

I have the following piece of code.

import pymysql

conn = pymysql.connect(server, user , password, "db")

cur = conn.cursor()

ORFs={'E7': '562', 'E6': '83', 'E1': '865', 'E2': '2756 '}

table="genome"

cols = ORFs.keys()

vals = ORFs.values()

sql = "INSERT INTO %s (%s) VALUES(%s)" % (

table, ",".join(cols), ",".join(vals))

print sql

print ORFs.values()

cur.execute(sql, ORFs.values())

cur.close()

conn.close()

the print sql statement returns

INSERT INTO genome (E7,E6,E1,E2) VALUES(562,83,865,2756 )

when I type this directly into the mysql command line, the mysql command works. But when I run the python script I get an error:

: not all arguments converted during string formatting

args = ('not all arguments converted during string formatting',)

message = 'not all arguments converted during string formatting'

As always, any suggestions would be highly appreciated.

解决方案sql = "INSERT INTO %s (%s) VALUES(%s)" % (

table, ",".join(cols), ",".join(vals))

This SQL includes values and cur.execute(sql, ORFs.values()) has values, too.

So, it should be cur.execute(sql).

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值