8

I am using mysql-connector with python and have a query like this:

SELECT avg(downloadtime) FROM tb_npp where date(date) between %s and %s and host like %s",(s_date,e_date,"%" + dc + "%")

NOw, if my variable 'dc' is a list like this:

 dc = ['sjc','iad','las']

Then I have a mysql query like below:

SELECT avg(downloadtime) FROM tb_npp where date(date) = '2013-07-01' and substring(host,6,3) in ('sjc','las');

My question is, how do I write this query in my python code which will convert my variable 'dc' to a list?

I tried the below query but getting error: Failed processing format-parameters; 'MySQLConverter' object has no attribute '_list_to_mysql'

cursor3.execute("SELECT avg(downloadtime) FROM tb_npp where date(date) between %s and %s and substring(host,6,3) in %s",(s_date,e_date,dc))

Can somebody please tell me what I am doing wrong?

Thanks in advance

2
  • if you just run the query without using python does it work? Commented Jul 25, 2013 at 1:06
  • Yes, the query is correct Commented Jul 25, 2013 at 1:07

3 Answers 3

14

I'm not familiar with mysql-connector, but its behavior appears to be similar to MySQLdb in this regard. If that's true, you need to use a bit of string formatting:

sql = """SELECT avg(downloadtime) FROM tb_npp where date(date) = %s 
         and substring(host,6,3) in ({c})""".format(
            c=', '.join(['%s']*len(dc)))    
args = ['2013-07-01'] + dc
cursor3.execute(sql, args)
Sign up to request clarification or add additional context in comments.

3 Comments

Ha.... THanks a lot.. :) it works, but if i have many args, can i just concatenate all of them to the args variable like: args = ['2013-07-01'] + ['2013-07-07'] + dc ?
Yes, or: args = ['2013-07-01', '2013-07-07'] + dc
I don't agree with this answer,In fact, we don't have to do anything, pymsql has already done it。For example: SELECT avg(downloadtime) FROM tb_npp where date(date) = %s and substring(host,6,3) %s
3

As an alternative to @unutbu's answer which is specific to using mysql-connector:

cursor.execute(
    "SELECT thing "
    "    FROM table "
    "    WHERE some_col > %(example_param)s " 
    "    AND id IN ({li})".format(li=", ".join(list_of_values)),
    params={
        "example_param": 33
    })

If you try to move the joined list into a param (like example param) it may complain because mysql-connector interprets the values as strings.

If your list isn't made up of things that are string-format-able (like integers) by default then in your join statement replace list_of_values with:

[str(v) for v in list_of_values]   

1 Comment

This is vulnerable to SQL injection.
0
SELECT avg(downloadtime) FROM tb_npp where date(date) = '2013-07-01' and substring(host,6,3) in %s 

That's it, that's it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.