1

I have a strange issue where I can't get an SQL query with parameter to work with a string comparison in the where clause - I don't get a row back. when i connect to the MySQL db via bash, the query works.

  • python 3.7.3
  • mysql-connector-python==8.0.11
  • mysql 5.7

works (getting my row):

select * from my_table where my_column = 'my_string';

also works (getting my row):

cursor.execute(
    """
        select *
        from my_table
        where my_column = 'my_string'
    """
)

doesn't work (cursor.fetchall() is []):

cursor.execute(
    """
        select *
        from my_table
        where my_column = '%s'
    """,
    ('my_string')
)
2
  • 2
    Doesn't work means what exactly? Do you get an error? Commented Jul 16, 2019 at 8:47
  • 1
    there was a comment that's now removed - writing ('my_string',) with a comma after the string. please add as answer so i can promote it as correct answer! Commented Jul 16, 2019 at 8:53

2 Answers 2

1

Be careful with tuples. I think you need ('my_string',).


FYI I wrote the original comment mentioned by @tscherg in his comment below the question.

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

1 Comment

this along with removing the quotes around %s did it!
1

Remove the quotes:

cursor.execute(
    """
        select *
        from my_table
        where my_column = %s
    """,
    ('my_string')
)

1 Comment

that's not it. getting a MySQL Error (42000) telling me to check the syntax near %s

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.