I've been trying to only insert new ids if they weren't inserted before in my database, but the problem I've been having is that I couldn't get the nested queries to work in Python Flask. Whatever I do, it just returns a syntax error.

from flask import Flask, flash, jsonify, redirect, render_template, request, session
from cs50 import SQL

# Configure application
app = Flask(__name__)

# Ensure templates are auto-reloaded
app.config["TEMPLATES_AUTO_RELOAD"] = True

db = SQL("sqlite:///database.db")

def index():
    ##shows the list of patients
    ## to do later
    return render_template("index.html")
    return 0

@app.route("/add", methods=["GET", "POST"])
def add():
    if request.method == "GET":
        return render_template("add.html")
        ### process the data inputted by the user into the database using form.request ( have to assign what values can be inputted)
        ## lab data: WBC(neut+lymph), Hb, Plt BS
        pt_id = request.form.get("id")
        name = request.form.get("name")
        Hb = request.form.get("Hb")
        Plt = request.form.get("Plt")
        Neut = request.form.get("Neut")
        Lymph = request.form.get("Lymph")
        WBC = request.form.get("WBC")
        db.execute("INSERT into patients (id, name) VALUES(?, ?) WHERE NOT EXISTS (SELECT pt_id from patients WHERE pt_id = ?)", pt_id, name, pt_id)
        db.execute("INSERT into CBC (id, Hb, Plt, Neut, Lymph, WBC) VALUES(?,?,?,?)", pt_id, Hb, Plt, Neut, Lymph, WBC)

1 Answer 1


Have a look at the sqlite doc on INSERT. There is no WHERE clause in an INSERT query. Perhaps logic similar to register in the finance project would be the right approach.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .