Skip to content
Advertisement

Any way to pass operators <, , >= into sql-query?

I have such piece of program:

What I want to do is write ‘<‘ in command in the same way as {table} and {pkey}, that means I want to pass operators into command from variable. Can I do it?

The face of the app looks like this A little bit more of code context. It’s an app, that should get data from database by sql-request, that creates from interface. As you can see, there’s a bit more operators than one to choose.

Advertisement

Answer

You can use string interpolation on comparison_command, use f-string notation, and double the existing braces to escape them:

This assumes that self.comp_selec has the operator in the SQL syntax, i.e. it should be like “<“, “<=”, “=”, “>=”, “>”, and not “==”, “ge”, “greater”, …etc.

I need to add the disclaimer about the risk of SQL injection. As I understand all of this runs on a local machine, a smart user could potentially tamper with the executable and make the SQL execute something harmful for the database or its security.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement