How to write query in Flask using placeholders to join tables

Viewed 77

I am trying to render information on different parts of the body to make html page in flask. My database includes a "body" table that has a row for each joint with a column for joint_key and another column for the name of the joint. Each joint has its own separate table that has a column for its key and various other information. I am trying to allow the user to select a joint and use their selection to join the body table to the specific join table so I can render all of the information. I've tried this.

def index():
if request.method == "GET":
    JOINTS = db.execute("SELECT name FROM body;")
    return render_template("index.html", joints=JOINTS)
if request.method == "POST":
    joint = request.form.get("joint")
    EX = db.execute("SELECT * FROM ? INNER JOIN body ON ?.joint_id=body.joint_key;", joint, joint)
    return render_template("prep.html", ex=EX)

I've also tried using the placeholder to go straight to the table needed like this:

joint = request.form.get("joint")
EX = db.execute("SELECT * FROM ? ", joint)
1 Answers

You have to use a tuple with your placeholders.

EX = db.execute("SELECT * FROM ? INNER JOIN body ON ?.joint_id=body.joint_key;", (joint, joint))
Related