araspion
araspion

Reputation: 713

PyMySQL Parametrized Query and Date Format

I'm trying to run the following query using PyMySQL

cur.execute("SELECT %s as label, UNIX_TIMESTAMP(CONVERT(DATE_FORMAT(date_added, '%%Y-%%m-%%d-%%H:%%i:00'),DATETIME)) as time, %s  as metric FROM %s WHERE id >= %d GROUP BY label, time", (label, metric, table, min_id,))

I am getting the following error:

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

label, metric, table, and min_id are a string, string, string, and int, respectively.

Am I not escaping the %'s correctly? Can't quite figure this out, have tried a few things.

Thanks!

Upvotes: 1

Views: 3432

Answers (1)

mata
mata

Reputation: 69042

... id >= %d ... should be ... id >= %s ....

All parameters are escaped converted to strings before applied to the format string.

Upvotes: 2

Related Questions