1

I use Python2.7 on Windows 7 and a mysql server, connection by pymssql.

My Problem: I have a very big Database and I like to select the ID's of objects matching one of several words(string) from a list, I give to my program. In this query there must be a LIKE %...% expression for these words of my list, too.

So far I connected my Python-Script to my Database and defined a cursor. Then I made a small list with the words, I am searching for and I created some placeholders for my query later:

wortliste = ['Bruch', 'Verwerfung']
placeholders = ','.join(['%s'] * len(wortliste))

Here is my Query:

query = """ SELECT BO_INDEX FROM GeoTest.dbo.Tabelle_Bohrung
        WHERE BO_BEMERKUNG IN ({})""".format(placeholders)

When I am searching for a single word, here for example for the word 'Bruch', my query would look like this:

query = """ SELECT BO_INDEX FROM GeoTest.dbo.Tabelle_Bohrung
        WHERE BO_BEMERKUNG LIKE '%Bruch%'"""

This query for a single word matches the right Id's (=BO_INDEX). The query with the placeholders doesn't crash, but it didn't match anything :(

But I like to loop my database for a couple of words and append the matching ID's for every word (string) in my list(=wortliste) and append it to a new list.

I really dont't know how to solve this problem!

I am grateful for every new way to solve this challenge! Thanks!

2 Answers 2

0

EDIT 2:

If you want to loop over your list and append to the output (using your example):

words = ['ab', 'cd', 'ef']
abfrage_list = []
for w in words:
    # Generate a query
    query = """ SELECT BO_INDEX FROM GeoTest.dbo.Tabelle_Bohrung
        WHERE BO_BEMERKUNG LIKE '%%%s%%' """ % w
    # Execute that query and get results
    cur.execute(query)
    result_all = cur.fetchall()
    # Add those results to your final list
    for i in result_all:
        abfrage_list.append(i)

EDIT:

For your example with multiple likes:

query = """ SELECT BO_INDEX FROM GeoTest.dbo.Tabelle_Bohrung
    WHERE BO_BEMERKUNG LIKE '%ab%'
     OR O_BEMERKUNG LIKE '%cd%' 
     OR O_BEMERKUNG LIKE '%ef%' """

query = """ SELECT BO_INDEX FROM GeoTest.dbo.Tabelle_Bohrung
    WHERE {params}""".format(
        params=" OR ".join("BO_BEMERKUNG LIKE '%%%s%%' \n" % w for w in wortliste)
    )
print(query)

Prints:

 SELECT BO_INDEX FROM GeoTest.dbo.Tabelle_Bohrung
    WHERE BO_BEMERKUNG LIKE '%Bruch%' 
 OR BO_BEMERKUNG LIKE '%Verwerfung%' 

Your placeholders doesn't contain any of the items from your word list, use:

placeholders = ','.join("'%s'" % w for w in wortliste)

For example:

wortliste = ['Bruch', 'Verwerfung']
print(','.join(['%s'] * len(wortliste)))
print(','.join("'%s'" % w for w in wortliste))

Prints:

%s,%s
'Bruch','Verwerfung'
Sign up to request clarification or add additional context in comments.

12 Comments

But my query still doesn't match anything, i have to include the LIKE '%...%' - expression, but I don't know how for this example.
Do you know what query you want to build, can you add that to the question? Maybe you would like to use this: stackoverflow.com/questions/12957993/…
Well, this is the first step of my program. The contents of my list will chance with every new query and because of that, i want to outsource this list in a next step for a user, who only types the words he is searching for in this outsourced list and runs the program! :)
Yes, so suppose you have a list ['Bruch', 'Verwerfung'], what would you want a sql query to look like for this list?
query = """ SELECT BO_INDEX FROM GeoTest.dbo.Tabelle_Bohrung WHERE BO_BEMERKUNG LIKE '%Bruch%'""" cur.execute(query) result_all=cur.fetchall() abfrage_list = [] for i in result_all: abfrage_list.append(i) and this procedure for any other word in my list!
|
0

for the Example of the following list = ['ab','cd','ef']

query = """ SELECT BO_INDEX FROM GeoTest.dbo.Tabelle_Bohrung
    WHERE BO_BEMERKUNG LIKE '%ab%'
     OR O_BEMERKUNG LIKE '%cd%' 
     OR O_BEMERKUNG LIKE '%ef%' """

cur.execute(query)
result_all = cur.fetchall()

abfrage_list = []
for i in result_all:
    abfrage_list.append(i)

But I need this procedure for possibly hundreds of strings in this list. i need to loop over this list and i need the LIKE expression in the query, otherwise it won't catch anything.

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.