problem with SQL query using python on cx_oracle

Status
You're currently viewing only ledbutter's posts. Click here to go back to viewing the entire thread.
Not open for further replies.
[url=http:/civis/viewtopic.php?p=28461379#p28461379:2fpiwywe said:
C0rinthian[/url]":2fpiwywe]
[url=http:/civis/viewtopic.php?p=28460831#p28460831:2fpiwywe said:
andrewme[/url]":2fpiwywe]You could simplify that to:

Code:
cr.execute("select * from table where column like 'pattern%'")

Since there is no user input involved here and the strings are all constant, there is no risk of SQL injection and no need to use bound parameters in this case.

It's a bad pattern to get in the habit of regardless. There is literally no downside to using parameters, so just use them everywhere and you never need to worry about remembering what might be coming from user input.

As long as we are making suggestions regarding best practices, I have two more:
  1. Why not turn this into a stored procedure? Then the whole 'pattern%' detail can be hidden away
  2. I'd suggest explicitly referencing each column name in the select instead of doing "select *" as if the schema changes, you could be hiding bugs by using "select *"
 
[url=http:/civis/viewtopic.php?p=28479639#p28479639:2l6j6oxa said:
C0rinthian[/url]":2l6j6oxa]
[url=http:/civis/viewtopic.php?p=28464611#p28464611:2l6j6oxa said:
ledbutter[/url]":2l6j6oxa]
[url=http:/civis/viewtopic.php?p=28461379#p28461379:2l6j6oxa said:
C0rinthian[/url]":2l6j6oxa]
[url=http:/civis/viewtopic.php?p=28460831#p28460831:2l6j6oxa said:
andrewme[/url]":2l6j6oxa]You could simplify that to:

Code:
cr.execute("select * from table where column like 'pattern%'")

Since there is no user input involved here and the strings are all constant, there is no risk of SQL injection and no need to use bound parameters in this case.

It's a bad pattern to get in the habit of regardless. There is literally no downside to using parameters, so just use them everywhere and you never need to worry about remembering what might be coming from user input.

As long as we are making suggestions regarding best practices, I have two more:
  1. Why not turn this into a stored procedure? Then the whole 'pattern%' detail can be hidden away
  2. I'd suggest explicitly referencing each column name in the select instead of doing "select *" as if the schema changes, you could be hiding bugs by using "select *"

The first is debatable. The second, I absolutely agree with.

Of course the first is debatable, but we'd at least take care of the concern over passing in 'pattern%' as in a sproc that could just be hard-coded in. I'd also advocate for it if there is a reason this query would ever change, as having it in a sproc would decouple those changes from the application (i.e. update the database without having to redeploy the app). If this is throwaway code or a simple tool, then sure, that probably isn't worth it.
 
Status
You're currently viewing only ledbutter's posts. Click here to go back to viewing the entire thread.
Not open for further replies.