Python MySQLdb issues (TypeError: %d format: a number is required, not str)
Asked Answered
N

6

76

I am trying to do the following insert operation:

cursor.execute("""
                    insert into tree (id,parent_id,level,description,code,start,end)
                    values (%d,%d,%d,%s,%s,%f,%f)
                    """, (1,1,1,'abc','def',1,1)
                    )

The structure of my MYSQL table is:

id int(255),
parent_id int(255),
level int(11),
description varchar(255),
code varchar(255),
start decimal(25,4),
end decimal(25,4)

However when I run my program, I get the error

" File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 151, in execute query = query % db.literal(args)

TypeError: %d format: a number is required, not str"

Nedranedrah answered 26/4, 2011 at 2:1 Comment(0)
L
182

The format string is not really a normal Python format string. You must always use %s for all fields.

refer official document:

If args is a list or tuple, %s can be used as a placeholder in the query. If args is a dict, %(name)s can be used as a placeholder in the query.

-> that is: here %s is NOT formatter, but is a placeholder

Laevo answered 26/4, 2011 at 2:3 Comment(8)
Saved me from wasting several more hours !! Thank you very much ! Is there any other way to handle prepared statements? Because this really does not seem be a prepared statement so to speak.Nedranedrah
Use oursql. It can do real parameterization, instead of fake client-side one MySQLdb does.Wernsman
Darn. I wasted a solid three hours trying to understand why %d is causing error..Twopence
I encountered this problem too. 我刚才也遇到了这个问题,看到回答,我就知道怎么做了Pollinosis
You must always use %s for all fields. 这句话最实用Pollinosis
I had to convert decimal values e.g. 1.52456 to string str(value) and then I could use %s. %d did not work even if I converted value to decimal using decimal.Decimal(). Finally I got this working.Bonaventura
Do you have a source for this? I trust you, and this solution worked, but I'd prefer to know an authoritative source and why they chose to mandate this.Newsboy
@Newsboy The source code itself. It's just a custom string interpolation implementation. It resembles Python, but isn't the normal Python. This is done to escape the value to prevent SQL injection.Laevo
C
5

I got the same error, but it was for a different reason. The problem was that the string included a '%' and that confused Python:

TemplateStr = '[....] % discount rate  [....]  %s and %s
print TemplateStr % ('abc', 'def')

Python interpreted that "% discount" as "%d" and kept complaining because I was feeding it a string ('abc'), not a number.

It took me a long time to figure it out!

(The solution is to type %% instead of %.)

Chappy answered 13/3, 2019 at 0:14 Comment(0)
R
3

Based on this answer https://mcmap.net/q/266799/-python-mysql-insert-float-variables-error

I finally use %s even for floats and digits. It will be converted right away when inserted to the table and SQL detected the table data type

Rexer answered 9/11, 2021 at 13:18 Comment(0)
P
0

try this :

cursor.execute("""
                    insert into tree (id,parent_id,level,description,code,start,end)
                    values (%s,%s,%s,'%s','%s',%s,%s)
                    """%(1,1,1,'abc','def',1,1)
                    )
Pesticide answered 12/8, 2013 at 14:19 Comment(0)
A
-1

Since his data is in integer or decimal format how can you use %s which is usually used for string format %d or %i should be used for integer or decimal values.

Agreement answered 21/1, 2017 at 10:36 Comment(0)
R
-1

Whenever you saw that type of errors, should use type() function for checking the types of values or variables...., and will see type is - 'str'. Means python takes all the values in strings (By default data type is string). That's why the error said %d is for numbers, Not for the strings. So consider %s in python for every types of field.

Resinate answered 3/10, 2018 at 18:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.