Вопрос

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.

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top