문제

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