Code to accept input from the user and gives a User for a given username. Now it is accepting the input directly into the SQL query but I would like to use it with prepared Statements, how could I do it?
# SELECT QUERIES
def get_all_results(q):
cur = mysql.connection.cursor()
cur.execute(q)
mysql.connection.commit()
data = cur.fetchall()
cur.close()
return data
# UPDATE and INSERT QUERIES
def commit_results(q):
cur = mysql.connection.cursor()
cur.execute(q)
mysql.connection.commit()
cur.close()
##### Returns a user for a given username
### in: username
### out: User
def get_user(username):
q = "SELECT * FROM Users"
q+= " WHERE username = '%s'" % (username)
logging.debug("get_user query: %s" % q)
data = get_all_results(q)
if len(data) == 1:
user = User(*(data[0]))
return user
else:
logging.debug("get_user: Something wrong happened with (username):(%s)" % (username))
return None```