skip to Main Content

I have a Flask app that is executing an sql query to retrieve a list of parts from a MySql database. My problem is that I am sure I have the SQL query correct, because it works in MySql workbench, but when I run the same query in the app is returns an empty list.

I am fairly new to this so go easy on me.

SQL query:

SELECT job_part.*, part.part_name, part.cost
  FROM job_part
  JOIN part ON job_part.part_id = part.part_id
 WHERE job_part.job_id = 5;

enter image description here

my Python

connection = getCursor()
sql = """SELECT job_part.*, part.part_name, part.cost
           FROM job_part
           JOIN part ON job_part.part_id = part.part_id
           WHERE job_part.job_id = %s;"""
connection.execute(sql, (jobNo,))
partList = connection.fetchall()
connection.close()
print(partList)

jobNo is 5 as a str, I have tried converting to int but no difference

I get [] in the terminal

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to all who offered help. I had been using mysql 8.2 and been getting a version mismatch error in workbench whenever I started it, and had been ignoring it, hadn't caused any obvious problems. In the end uninstalling mysql and workbench and installing v8.0 for both resolved this issue. Lesson leanred.


  2. Try to narrow down the cause of the problem. Could be:

    1. a db connection issue, for instance libraries or drivers
    2. a problem with the variable string substitution
    3. a problem with query column or table aliases (the fact that you are using a * and using the same table name as an alias instead of t1 and t2 which is more common)

    I’m leaning for (2). Try to follow these troubleshooting steps: change the query and use a static job_id. If that works, then you know it’s got to do with variable substitution.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search