MySQLdb Python inserts% d and% s

predecessor:

MySQL Table created via: CREATE TABLE table(Id INT PRIMARY KEY NOT NULL, Param1 VARCHAR(50)) 

Function:

 .execute("INSERT INTO table VALUES(%d,%s)", (int(id), string) 

Exit:

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

I am not sure what is going on here or why I cannot execute the command. This uses MySQLdb in Python. .execute is executed on the cursor object.

EDIT:

Question: Python MySQLdb releases (Error like:% d format: number required, not str) says that you should use %s for all fields. Why could this be? Why does this command work?

 .execute("INSERT INTO table VALUES(%s,%s)", (int(id), string) 
+12
python mysql mysql-python
source share
3 answers

Since the whole request must be in string format at the time of the request, therefore %s should be used ...

After the query is completed, an integer value is stored.

So your line should be.

 .execute("INSERT INTO table VALUES(%s,%s)", (int(id), string)) 

Explanation here

+20
source share

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

+6
source share

You missed the '%' in string formatting

 "(INSERT INTO table VALUES(%d,%s)"%(int(id), string)) 
-2
source share

All Articles