Search code examples
pythonmysqlpymysql

PyMySQL Join Query is empty


I have a query that works perfectly in plain MySQL:

SELECT * FROM `ls_matches` AS m
LEFT JOIN `ls_regions` AS r
  ON r.id = m.region
WHERE
 m.crawled = FALSE

I am using PyMySQL lib to make a query, but the "same" query is empty:

sql = "SELECT * FROM `ls_matches` AS m"\
            "LEFT JOIN `ls_regions` AS r"\
                "ON r.id = m.region"\
            "WHERE"\
            "m.crawled = %s"
        cursor.execute(sql, (False,))
        results = cursor.fetchall()
        pprint(results)

Any ideas on why this is empty? W/out the JOIN it works perfectly, so the issue is somewhere with that I think!

Any hints highly appreciated!


Solution

  • You need to check errors.

    How about this?

    sql = """
    SELECT *
    FROM `ls_matches` m LEFT JOIN
         `ls_regions` r
         ON r.id = m.region
    WHERE m.crawled = '%s'
    """
    

    Python gives you the ability to have strings that span multiple lines. Use the capabilities of the language.

    I would also encourage you to use parameters rather than stuffing values into the string, but that is a different issue.