Skip to content

Instantly share code, notes, and snippets.

@dimasahmad
Created November 18, 2021 09:24
Show Gist options
  • Save dimasahmad/b67f175c8aab919ac18ef33656f170d0 to your computer and use it in GitHub Desktop.
Save dimasahmad/b67f175c8aab919ac18ef33656f170d0 to your computer and use it in GitHub Desktop.
sql.py
from flask import request
# ?location=Bandung&gender=male
filter_by = {"location": "Bandung", "gender": "male"}
where = []
for f, v in filter_by.items():
if f == "location":
where.append("location LIKE '%{}% ".format(v))
if f == "gender":
if v == "male":
where.append("gender != female")
if v == "female":
where.append("gender != male")
column = "jobs_id, jobs_name, c_user_id, jobs_description, jobs_location, jobs_type, jobs_gender, jobs_status"
table = "jobs INNER JOIN users ON users.user_id = jobs.c_user_id"
sql = "SELECT %s FROM %s WHERE %s" % (column, table, " AND ".join(where))
print(sql)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment