Forum Stats

  • 3,816,115 Users
  • 2,259,144 Discussions
  • 7,893,390 Comments

Discussions

Executing entire sql script file from cx_Oracle

675949
675949 Member Posts: 4
edited Jan 27, 2009 12:27AM in Python
Is there a method in cx_Oracle to execute an entire sql script file?

Answers

  • 451875
    451875 Member Posts: 14
    If you mean a SQL*Plus script, then no. If you have a text file containing a series of SQL statements you could do something like this;
    statements = open('script_file.sql').readlines()
    statements = ''.join(statements)
    sql_statements = statements.split(';')
    orcl = cx_Oracle.connect('connection string')
    cursor = orcl.cursor()
    for sql in sql_statements:
    ... cursor.execute(sql)
    ...

    Although reading and executing code from arbitrary files isn't a terribly great idea. Added to the fact that you have no ability to handle or pass arguments or parameters and this is probably only useful as a once off.
This discussion has been closed.