Domanda

I have been using cx_Oracle to perform SQL queries on an Oracle database in Python. So far I have been pasting these queries into strings and then running them using the cursor.execute() function that comes with cx_Oracle:

#simple example
query = """SELECT *
           FROM my_table"""
cursor.execute(query)

However, my select queries have gotten quite complex, and the code is starting to look a bit messy. I was wondering if there were any way to simply save the SQL code into a .sql file and for Python or cx_Oracle to call that file? I thought something like that might be simple to find using Google, but my searches are oddly coming up dry.

È stato utile?

Soluzione

Well, you can certainly save SQL code to a file and load it:

query = open('foo.sql', 'r').read()
cursor.execute(query)

I can't find any reference to saved queries in cx_Oracle, so that may be your best bet.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top