I'm trying to execute a query using two parameters, one is a list and the other is a number.
Here is my code:
cursor.execute("SELECT cod_art, price_EU, price_third_country
FROM lt_gamma_articles
WHERE cod_art in (%s) AND cod_store = %d
ORDER BY cod_art, timestamp"
% format_strings, tuple(cod_art_int), int(shop) )
I get this error:
TypeError: not enough arguments for format string
I think the error is in the string formatting, but I don't know how to format it correctly and I've been stuck for a while.
From the looks of your code, I'm gussing your basing it off of imploding a list for use in a python MySQLDB IN clause. Assuming that's the case, and format_strings is built similar to:
I would use .format to build the query string, and build up a list for the positional query parameters:
Explanation:
Say
cod_art_intis this list of integer values:In order to use those values in the
cod_art IN (...)part of the query, you need to add a%sfor each one. That is done by:Which can be broken down to:
When you build the final query string, you replace
{cod_art_list}withformat_strings:And you get the query string:
Then your query parameters will be safely substituted within the query to replace the
%ss. You build up the parameter list to correspond to the%ss. Sincecod_art IN (%s,%s,%s)is first, you add that to the list first, followed by the value forcod_store(int(shop)which I'm going to say is 456):In the end you execute the query with its parameters: