Search code examples
pythonsql-serverpypyodbc

Use SQL 'like' in Pandas with input()


so I'm using the code below to SELECT columns FROM a certain table in SQL Server.

sus_base = pd.read_sql_query("SELECT screen_name, user_id, text FROM [dbo].[TABLE_ONE]", con)
df1 = pd.read_sql_query("SELECT screen_name, user_id, text FROM [dbo].[TABLE_ONE] WHERE text like '%car%'", con)
df2= pd.read_sql_query("SELECT screen_name, user_id, text FROM [dbo].[TABLE_ONE] WHERE text like '%plane%'", con)
df3= pd.read_sql_query("SELECT screen_name, user_id, text FROM [dbo].[TABLE_ONE] WHERE text like '%quality%'", con)

But, I have to do this matching for a lot of strings. I tried the following in Python which did not work:

test = pd.read_sql_query(("SELECT screen_name, user_id, text FROM [dbo].[TABLE_ONE] WHERE text like '%s%'", input()), con)

It gives me the following error, after asking me for input:

DatabaseError: Execution failed on sql '("SELECT screen_name, user_id, text FROM [dbo].[TABLE_ONE] WHERE text like '%s%'", "'car'")': bytes or integer address expected instead of tuple instance. 

And yes, I also tried it without the '' quotes around car.

df1 would then look like:

JAN | 20938 | Hi I am looking for a car somewhere
BOB | 38470 | Guys, where is my plane?
ELSA| 49850 | Never know what to do next..
CARL| 08923 | The quality of this product is amazing!
ETC.

You can imagine it would be a lot of work to create this. So it would be nice if the end-user can just type in the word he is looking for in the string of texts and it would return those texts like it would when I type them in the example above. Then I'll make sure it gets into a dataframe and/or Excel file.


Solution

  • The input is not being translated into text that pandas needs to send the query. In the line:

    pd.read_sql_query(("SELECT ... WHERE text like '%s%'", input()), con)
    

    What is actually being fed is pd.read_sql_query((str, str), con). In other word, you sent a 2-tuple of strings and the connection object, it expects just a string and the connection object. Try formatting the string instead:

    pd.read_sql_query("SELECT ... WHERE text like %{}%".format(input()), con)