I am making a web app for my final project. In my code, a strange syntax error is occurring in the middle of a SQL query. Please, may somebody help?
Code for that route (problematic line is surrounded by stars):
@app.route("/users/edit", methods=["GET", "POST"])
@login_required
def edit_users():
id = session['user_id']
introle = int(db.execute("SELECT role FROM users WHERE userId = :id;", id=id)[0]['role'])
if introle == 1:
role = 'admin'
elif introle == '2':
role = 'engineer'
if request.method == "GET":
admin = db.execute("SELECT userName FROM users WHERE role = 1;")
admins = []
for ad_min in admin:
admins.append(str(ad_min['userName']).title())
southern = db.execute("SELECT userName FROM users JOIN provinces WHERE provinces.provinceId = users.provinceId AND provinces.provinceName = 'southern';")
southerns = []
for south_ern in southern:
southerns.append({"name": str(south_ern['userName']).title(), "id": int(south_ern['userId'])})
northern = db.execute("SELECT userName FROM users JOIN provinces WHERE provinces.provinceId = users.provinceId AND provinces.provinceName = 'northern';")
northerns = []
for north_ern in northern:
northerns.append(str({"name": str(north_ern['userName']).title(), "id": int(north_ern['userId'])})
**northwestern = db.execute("SELECT userName FROM users JOIN provinces WHERE provinces.provinceId = users.provinceId AND provinces.provinceName = 'northwestern';")**
northwesterns = []
for north_west_ern in northwestern:
northwesterns.append({"name": str(north_west_ern['userName']).title(), "id": int(north_west_ern['userId'])})
eastern = db.execute("SELECT userName FROM users JOIN provinces WHERE provinces.provinceId = users.provinceId AND provinces.provinceName = 'eastern';")
easterns = []
for east_ern in eastern:
easterns.append(str({"name": str(east_ern['userName']).title(), "id": int(east_ern['userId'])})
lusakan = db.execute("SELECT userName FROM users JOIN provinces WHERE provinces.provinceId = users.provinceId AND provinces.provinceName = 'lusaka';")
lusakans = []
for lusak_an in lusakan:
lusakans.append({"name": str(lusak_an['userName']).title(), "id": int(lusak_an['userId'])})
central = db.execute("SELECT userName FROM users JOIN provinces WHERE provinces.provinceId = users.provinceId AND provinces.provinceName = 'central';")
centrals = []
for cent_ral in central:
centrals.append(str({"name": str(cent_ral['userName']).title(), "id": int(cent_ral['userId'])})
luapulan = db.execute("SELECT userName FROM users JOIN provinces WHERE provinces.provinceId = users.provinceId AND provinces.provinceName = 'luapula';")
luapulans = []
for luapul_an in luapulan:
luapulans.append({"name": str(luapul_an['userName']).title(), "id": int(luapul_an['userId'])})
copperbelt = db.execute("SELECT userName FROM users JOIN provinces WHERE provinces.provinceId = users.provinceId AND provinces.provinceName = 'copperbelt';")
copperbelts = []
for copper_belt in copperbelt:
copperbelts.append({"name": str(copper_belt['userName']).title(), "id": int(copper_belt['userId'])})
western = db.execute("SELECT userName FROM users JOIN provinces WHERE provinces.provinceId = users.provinceId AND provinces.provinceName = 'western';")
westerns = []
for west_ern in western:
westerns.append({"name": str(west_ern['userName']).title(), "id": int(west_ern['userId'])})
return render_template("editusers.html", user_id=role, admins=admins, southerns=southerns, northerns=northerns, northwesterns=northwesterns, easterns=easterns, lusakans=lusakans, centrals=centrals, luapulans=luapulans, copperbelts=copperbelts, westerns=westerns)
elif request.method == "POST":
tnumber = request.form.get("number")
if tnumber:
number = int(tnumber)
name = request.form.get("name")
if name:
db.execute("UPDATE users SET userName = :name WHERE userId = :id", id=number)
tpassword = request.form.get("password")
if tpassword:
password = generate_password_hash(tpassword)
db.execute("UPDATE users SET password = :password WHERE userId = :id", id=number)
tprovince = request.form.get("province")
if tprovince:
if tprovince == 10:
province = None
else:
province = tprovince
db.execute("UPDATE users SET province = :province WHERE userId = :id", id=number)