<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: cx_Oracle Python List Querying Question in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/cx-oracle-python-list-querying-question/m-p/69813#M5720</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Hello and thank you for replying. Actually, query1 works. However, the other two do not work. I assume it has something to do with the functionality of cursor.execute function, or the syntax of the code (SQL, or Python) I'm attempting to execute. I am in an Oracle 11g environment, if that helps at all.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Do those queries actually work in an actual query (outside of your python environment like SQL Developer)?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Edit: I don't think your SQL syntax is correct in query2 and query3 and has nothing to do with your cursor.&amp;nbsp; You should go into SQL Developer to re-write and test to make sure it is returning the correct information.&amp;nbsp; Once you have that completed then just copy/paste into your python script.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 16 Jun 2014 17:14:26 GMT</pubDate>
    <dc:creator>JamesCrandall</dc:creator>
    <dc:date>2014-06-16T17:14:26Z</dc:date>
    <item>
      <title>cx_Oracle Python List Querying Question</title>
      <link>https://community.esri.com/t5/python-questions/cx-oracle-python-list-querying-question/m-p/69810#M5717</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hey Everyone,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import cx_Oracle
con = cx_Oracle('authentication string')
cur = con.cursor()

IDs = ('A1', 'A2', 'A3', 'A4')

query1 = """
SELECT * 
FROM table
WHERE ID IN """+str(IDs)

Q = cur.execute(query1)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;However, if I attempt to join a table in the SQL statement I get the following error: ORA-00907: missing right parenthesis, like so:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;query2 = """
SELECT * 
FROM (
SELECT t1.ID, t2.*
FROM table1 as t1, table2 as t2
WHERE t1.ID = t2.ID)
WHERE ID IN """+str(IDs)

Q = cur.execute(query2)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;query3 = """
SELECT t1.new_ID as new_ID, t2.*
FROM table1 as t1, table2 as t2
WHERE t1.ID = t2.ID
AND t1.new_ID IN """ +str(IDs)

Q = cur.execute(query3)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 16 Jun 2014 15:53:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cx-oracle-python-list-querying-question/m-p/69810#M5717</guid>
      <dc:creator>DPolis</dc:creator>
      <dc:date>2014-06-16T15:53:14Z</dc:date>
    </item>
    <item>
      <title>Re: cx_Oracle Python List Querying Question</title>
      <link>https://community.esri.com/t5/python-questions/cx-oracle-python-list-querying-question/m-p/69811#M5718</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Can you wrap the code snippets into &lt;/SPAN&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt; brackets?&amp;nbsp; Use the "#" tool in the posting toolset above.

&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import cx_Oracle

con = cx_Oracle('authentication string')
cur = con.cursor()
 
IDs = ('A1', 'A2', 'A3', 'A4')
 
query1 = """SELECT * FROM table WHERE ID IN """ + str(IDs)
 
Q = cur.execute(query1)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'd first start by printing query1 to see the full query string that you will be passing in to build the cursor and hopefully you will be able to immediately see the problem.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Just as a quick test, you can try this version:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
 
query1 = """SELECT * FROM table WHERE ID IN (""" + str(IDs) + """)"""
 
&lt;/PRE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:40:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cx-oracle-python-list-querying-question/m-p/69811#M5718</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-12-10T22:40:40Z</dc:date>
    </item>
    <item>
      <title>Re: cx_Oracle Python List Querying Question</title>
      <link>https://community.esri.com/t5/python-questions/cx-oracle-python-list-querying-question/m-p/69812#M5719</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello and thank you for replying. Actually, query1 works. However, the other two do not work. I assume it has something to do with the functionality of cursor.execute function, or the syntax of the code (SQL, or Python) I'm attempting to execute. I am in an Oracle 11g environment, if that helps at all.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 16 Jun 2014 17:00:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cx-oracle-python-list-querying-question/m-p/69812#M5719</guid>
      <dc:creator>DPolis</dc:creator>
      <dc:date>2014-06-16T17:00:41Z</dc:date>
    </item>
    <item>
      <title>Re: cx_Oracle Python List Querying Question</title>
      <link>https://community.esri.com/t5/python-questions/cx-oracle-python-list-querying-question/m-p/69813#M5720</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Hello and thank you for replying. Actually, query1 works. However, the other two do not work. I assume it has something to do with the functionality of cursor.execute function, or the syntax of the code (SQL, or Python) I'm attempting to execute. I am in an Oracle 11g environment, if that helps at all.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Do those queries actually work in an actual query (outside of your python environment like SQL Developer)?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Edit: I don't think your SQL syntax is correct in query2 and query3 and has nothing to do with your cursor.&amp;nbsp; You should go into SQL Developer to re-write and test to make sure it is returning the correct information.&amp;nbsp; Once you have that completed then just copy/paste into your python script.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 16 Jun 2014 17:14:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cx-oracle-python-list-querying-question/m-p/69813#M5720</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2014-06-16T17:14:26Z</dc:date>
    </item>
    <item>
      <title>Re: cx_Oracle Python List Querying Question</title>
      <link>https://community.esri.com/t5/python-questions/cx-oracle-python-list-querying-question/m-p/69814#M5721</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Well, after much trial and error I figured out that the 'AS' table alias naming convention is simply not recognized in my current environment cx_Oracle version 5.1.2, Oracle 11.1, python 2.7.&amp;nbsp; Query 3 was the syntax I settled with as it was more straightforward, and didn't require a nested subquery. Thanks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;PS. Yes, a version of each query was tested in SQLPlus and they worked.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Jun 2014 12:20:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cx-oracle-python-list-querying-question/m-p/69814#M5721</guid>
      <dc:creator>DPolis</dc:creator>
      <dc:date>2014-06-17T12:20:37Z</dc:date>
    </item>
  </channel>
</rss>

