How not to do it:
id = raw_input("Enter the Station ID")
query = "select foo from bar where station={station_id}"
cursor.execute(query.format(station_id=id))
If someone enters a malicious sql string, it will be executed.
Instead of using python to format the string, let the database backend handle it for you. Exactly how you do this depends on the database you're using. I think (?) this is correct for Oracle, but I can't test it. Some databases use different characters (e.g. ?
instead of %s
in the case of SQLite).
id = raw_input("Enter the Station ID")
query = "select foo from bar where station=%s"
cursor.execute(query, [id])
Edit: Apparently, cx_Oracle
defaults to a "named" paramstyle (You can check this by having a look at cx_Oracle.paramstyle
.). In that case, you'd do something like this:
query = "select foo from bar where station=:station_id"
cursor.execute(query, station_id=id)