I have to use a non DBAPI-compliant library to interact with a database (qds_sdk for Qubole). This library only allows to send raw SQL queries without parameters. Thus I would like a SQL injection-proof way to insert parameters into a query and get the resulting formatted query in Python. Something like the format_sql function in the example below:
sql = 'select * from table where id = ?'
formatted_sql = format_sql(sql, (123,)) # 'select * from table where id = 123'
Is this possible at all or is it too RDBMS-specific?
I don't know too much about Quoble and the dialect of SQL it accepts and there is a whole range of datatype you might be dealing with. But in many cases converting the argument to a string and then escaping single quote characters by either doubling them up or preceding them with a backslash (MySQL, for example, allows both methods), is probably the best you can do. I would use
%sas the placeholder for your pseudo-prepared statement:Prints:
If there is any possibility of
%sappearing in your SQL in some context other than as a placeholder, then you need to place the single quotes around those occurrences that are actual placeholders and not have functionformat_sqlperform that function: