简体   繁体   中英

About python mysqldb TypeError

I don't know what is the problem. Can you help?

cur = conn.cursor()
sql = "select count(*) from sbox_trig where date > CURDATE() and name ='bw109nocd%2Ezip'"
cur.execute(sql)

Exception Type: TypeError
Exception Value: not enough arguments for format string

Double the % percent symbol:

sql = "select count(*) from sbox_trig where date > CURDATE() and name ='bw109nocd%%2Ezip'"

The %2E part is being interpreted as a string formatter for a floating point number otherwise.

You may have actually found a bug in the MySQLdb module. I suspect it is recognizing the percent sign in the string literal. Try the same query with the % removed (and nothing else changed) to verify if this is the case or not. If it is, you should probably post a message in the bug database for the module.

Also, if that is the source of the problem, the short-term fix is to use two % symbols (ie bw109nocd%%2Ezip) so that the string interpreter will see that as a literal percent sign.

Note also, if you want useful information here, you should include the version of Python, MySQL, and MySQLdb; it might be helpful to include also the operating system you are using, though it's probably not the most important thing here. For database issues, it would also be helpful to see at least the table schema.

I just tested a variation of your code on one of my databases, with no error; I'm running Ubuntu 12.04, with Python 2.7.3 and whatever the stock versions of MySQL and MySQLdb are.

I don't use MySQL for Python, but if I had to guess, it's interpreting the %2E as a printf-style format argument (see this page ) and expecting a parameter. You should probably try something like:

cur = conn.cursor()
sql = "select count(*) from sbox_trig where date > CURDATE() and name = %s"
cur.execute(sql, ("bw109nocd%2Ezip",))

Instead of specifying data directly in query, its better to first store it in some local variable and then pass the variable in the query.

cur = conn.cursor()

temp = "bw109nocd%2Ezip"

sql = "select count(*) from sbox_trig where date > CURDATE() and name = '%s' " % temp

cur.execute(sql)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM