mysql not enough arguments_not enough arguments for format string from excel to mysql

I notice 2 things that could cause this error:

Your variable values is a tuple already, so you dont need to wrap it inside a new list.

That means, change this line mycursor.execute(query, [values])

to mycursor.execute(query, values)

You are also missing a comma in your query in the part where you list the target column names, between penalty_way and penalty.

In case of this many arguments, I would suggest to restructure your code so that you can more easily see if you missed anything.

For example, here is a version that groups the 15 parameters in a 1-3-3-3-3-2 formation in 3 parts: the first part of the query, the second part of the query and also when building the values tuple.

query = """

INSERT INTO sanction (

id,

organization_type, organization, date,

decision_number, penalty_type, penalty_way,

penalty, violation, execution_period,

article, note, type_npa,

department, uploaded_date)

VALUES (

null,

%s, %s, %s,

%s, %s, %s,

%s, %s, %s,

%s, %s, %s,

%s, %s)

"""

for r in range(1, sheet.nrows):

organization_type = sheet.cell(r, 1).value

organization = sheet.cell(r, 2).value

date = sheet.cell(r, 3).value

decision_number = sheet.cell(r, 4).value

penalty_type = sheet.cell(r, 5).value

penalty_way = sheet.cell(r, 6).value

penalty = sheet.cell(r, 7).value

violation = sheet.cell(r, 8).value

execution_period = sheet.cell(r, 9).value

article = sheet.cell(r, 10).value

note = sheet.cell(r, 11).value

type_npa = sheet.cell(r, 12).value

department = sheet.cell(r, 13).value

uploaded_date = datetime.now().strftime("%Y-%m-%d %H:%M")

values = (

# the first value of the INSERT statement will be NULL

organization_type, organization, date, # 3 elements

decision_number, penalty_type, penalty_way, # 3 elements

penalty, violation, execution_period, # 3 elements

article, note, type_npa, # 3 elements

department, uploaded_date, # 2 elements

)

mycursor.execute(query, values)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值