0

I am trying to run multiple SQL queries where the LIKE operator contains every element of a list in Python. I have a long list of strings

l = ['string1', 'string2', 'string3',....]

query_1 = "SELECT COUNT(request) FROM table WHERE request LIKE 'l[0]' AND DATE BETWEEN 'date1' AND 'date2'".format(l[0])
query_2 = "SELECT COUNT(request) FROM table WHERE request LIKE 'l[1]' AND DATE BETWEEN 'date1' AND 'date2'".format(l[1])
query_3 = "SELECT COUNT(request) FROM table WHERE request LIKE 'l[2]' AND DATE BETWEEN 'date1' AND 'date2'".format(l[1])

and so on.

How would I run multiple queries and then concatenate them? Is it possible to use the execute function on multiple queries or can it only work with one? Would I use a for loop?

EDIT: In the previous version of this question, I asked about running ONE query with multiple list elements in the LIKE operator. I realized that the final query was something along the lines of:

SELECT COUNT(request) FROM table WHERE request LIKE 'l[0]' OR LIKE 'l[1]' OR LIKE 'l[2]' AND DATE BETWEEN 'date1' AND 'date2'.

That was not what I needed.

6
  • what is your question? Commented Aug 4, 2020 at 14:35
  • why do u want to run the commands simultaneously? have u tried executescript method for the cursor? Commented Aug 4, 2020 at 14:40
  • I apologize for any confusion, I edited the question so that I want to run multiple queries and then concatenate the results into one list. Commented Aug 4, 2020 at 14:43
  • 1
    just run the query and append the result to list Commented Aug 4, 2020 at 14:45
  • yes it is python, it is line by line interpretter u jus have to run the code and then append both the results. But should u be using formatted strings or place holders? Commented Aug 4, 2020 at 14:51

2 Answers 2

1

In case you are not looking for parallel execution just do the below (pseudocode)

l = ['string1', 'string2', 'string3',....]

query_1 = "SELECT COUNT(request) FROM table WHERE request LIKE 'l[0]' AND DATE BETWEEN 'date1' AND 'date2'".format(l[0])
query_2 = "SELECT COUNT(request) FROM table WHERE request LIKE 'l[1]' AND DATE BETWEEN 'date1' AND 'date2'".format(l[1])
query_3 = "SELECT COUNT(request) FROM table WHERE request LIKE 'l[2]' AND DATE BETWEEN 'date1' AND 'date2'".format(l[1])
result = [] 
query_lst = [query_1 ,query_2 ,query_3]
for query in query_lst:
   temp = query.execute()
   result.append(temp) 

If you are interested in parallel execution - see this example

https://pythonprogramming.net/values-from-multiprocessing-intermediate-python-tutorial/

Sign up to request clarification or add additional context in comments.

Comments

1

You can use f-strings with for loop

l = ['string1', 'string2', 'string3']
res = []
for x in l:
    query = f"SELECT COUNT(request) FROM table WHERE request LIKE '{x}' AND DATE BETWEEN 'date1' AND 'date2'"
    cursor.execute(query)
    res.append(cursor.fetchone()[0])

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.