Thứ Tư, 7 tháng 6, 2017

cx_Oracle Python List Querying with IN or any string


Hey Everyone,

So, I'm trying to query a very large table and would like to limit the results using a python list of field values, which I can successfully execute using the cx_Oracle cursor.execute function, like so

  1. import cx_Oracle  
  2. con = cx_Oracle('authentication string')  
  3. cur = con.cursor()  
  4.   
  5. IDs = ('A1', 'A2', 'A3', 'A4')  
  6.   
  7. query1 = """  
  8. SELECT *   
  9. FROM table  
  10. WHERE ID IN """+str(IDs)  
  11.   
  12. Q = cur.execute(query1)  


However, if I attempt to join a table in the SQL statement I get the following error: ORA-00907: missing right parenthesis, like so:

  1. query2 = """  
  2. SELECT *   
  3. FROM (  
  4. SELECT t1.ID, t2.*  
  5. FROM table1 as t1, table2 as t2  
  6. WHERE t1.ID = t2.ID)  
  7. WHERE ID IN """+str(IDs)  
  8.   
  9. Q = cur.execute(query2)  



Plus, when I attempt to run the query without the nested subquery I get the following error: ORA-00933: SQL command not properly ended, like so

  1. query3 = """  
  2. SELECT t1.new_ID as new_ID, t2.*  
  3. FROM table1 as t1, table2 as t2  
  4. WHERE t1.ID = t2.ID  
  5. AND t1.new_ID IN """ +str(IDs)  
  6.   
  7. Q = cur.execute(query3)  


I would appreciate any advice in this matter. I am new to using the cx_Oracle module. I assume that this is a result of user error, ie I'm coding something wrong. Thanks!

Không có nhận xét nào:

Đăng nhận xét