problem with SQL query using python on cx_oracle

Status
Not open for further replies.
I been googling a good chunk of the day looking for any kind of examples on how to accomplish what i am trying to do.

I had a python script that needs to access an oracle 11g database and run a select statement to pull back some records. The select statement is pretty much a simple:

Code:
select * from table where column like 'pattern%'


there is how i have my python code setup:

Code:
import cx_Oracle
c = cx_Oracle.connect('schema/password@dbhost/service')
cr = c.cursor()
cr.execute('select * from table where column like pattern%')

it blows up with an invalid character oracle error, which it's complain about the percent sign being in the query. I've tried escaping it and doing all sorts of stuff to try and get it to work, but i am all out of ideas.

has anyone worked with this module before and know what i am doing wrong?
 

C0rinthian

Ars Tribunus Angusticlavius
6,858
It's blowing up because you need to quote strings in SQL queries.

Code:
select * from table where column like pattern%;
vs.
Code:
select * from table where column like 'pattern%';

Regardless, you should really be parameterizing values. I think something like the following should work:

Code:
import cx_Oracle
c = cx_Oracle.connect('schema/password@dbhost/service')
query = 'select * from table where column like :0'
cr = c.cursor()
cr.execute(query, ('pattern%'))

Reference: http://www.oracle.com/technetwork/articles/dsl/prez-python-queries-101587.html
(Scroll down to Bind Variable Patterns)
 
thanks for pointing me down that path, i'll take a look at bind variables a little closely. I actually got it to work by stumbling across this thread on stackoverflow while looking up some more info on bind variables

http://stackoverflow.com/questions/1639 ... ame-number

especially this part of the correct response:

There are a few places where you are attempting to build LIKE clauses by concatenating bind variable placeholders. This concatenation can still be done, but it must be done in SQL using the || operator instead. So, instead of writing ':workarea%', write :workarea || '%', and insted of writing ':jobno_:qcActivity%', write :jobno || '_' || :qcActivity || '%'.

What i ended up doing is this:

Code:
import cx_Oracle
c = cx_Oracle.connect('schema/password@dbhost/service')
cr = c.cursor()
cr.execute("""select * from table where column like 'pattern' || '%'""")

which worked. Thanks!
 

C0rinthian

Ars Tribunus Angusticlavius
6,858
[url=http:/civis/viewtopic.php?p=28460831#p28460831:173uu1jo said:
andrewme[/url]":173uu1jo]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.
 
[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 *"
 
I agree with ledbutter... as well as others above.

Doing a SELECT * is bush league unless you really do need all of the columns (and even then, what if the table gets a new column?). When the size of this app grows you will kick yourself if you are taking too much of what you need and not utilizing the proper indexes (indicies?) to do so.
 

C0rinthian

Ars Tribunus Angusticlavius
6,858
[url=http:/civis/viewtopic.php?p=28464611#p28464611:1mcrhln3 said:
ledbutter[/url]":1mcrhln3]
[url=http:/civis/viewtopic.php?p=28461379#p28461379:1mcrhln3 said:
C0rinthian[/url]":1mcrhln3]
[url=http:/civis/viewtopic.php?p=28460831#p28460831:1mcrhln3 said:
andrewme[/url]":1mcrhln3]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.
 
[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.
 
[url=http:/civis/viewtopic.php?p=28465767#p28465767:3nlboh5a said:
FoamingPipeSnake[/url]":3nlboh5a]I agree with ledbutter... as well as others above.

Doing a SELECT * is bush league unless you really do need all of the columns (and even then, what if the table gets a new column?). When the size of this app grows you will kick yourself if you are taking too much of what you need and not utilizing the proper indexes (indicies?) to do so.

it's only an example, I'm only grabbing two columns in my python script.

Someone mentioned turning this into a stored procedure..not sure if i want to do that. This is a 3rd party application's database. I really don't want to be adding shit into it without potentially fighting with their support **cough Oracle cough**.

This script is also not accepting any user input, but i'd still will look into using parameters at some point as some suggested.
 

Fristie Blade

Ars Tribunus Militum
1,722
Subscriptor++
[url=http:/civis/viewtopic.php?p=28484969#p28484969:ywkcv2kh said:
madmanX[/url]":ywkcv2kh]
[url=http:/civis/viewtopic.php?p=28465767#p28465767:ywkcv2kh said:
FoamingPipeSnake[/url]":ywkcv2kh]I agree with ledbutter... as well as others above.

Doing a SELECT * is bush league unless you really do need all of the columns (and even then, what if the table gets a new column?). When the size of this app grows you will kick yourself if you are taking too much of what you need and not utilizing the proper indexes (indicies?) to do so.

it's only an example, I'm only grabbing two columns in my python script.

Someone mentioned turning this into a stored procedure..not sure if i want to do that. This is a 3rd party application's database. I really don't want to be adding shit into it without potentially fighting with their support **cough Oracle cough**.

This script is also not accepting any user input, but i'd still will look into using parameters at some point as some suggested.

Not owning the db nor wanting to pollute it with objects like this case is clearly a good reason to not use a stored procedure, which otherwise would be nicer although debatable.
But I agree with Corinthian: you never ever string concatenate input values into a parameter.
Using parameter bindings is the only good practice if you take input from an external source.

Since you hardcode your parameter instead of receiving it externally, it would not really matter though. Unless later on some one expands your script (business change) and builds upon what you wrote initially. So i suggest parameter binding anyway.

Would be (based upon comments above):
Code:
cr.execute("select * from table where column like :0 || '%' ", "pattern")
 
Status
Not open for further replies.