Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7788243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:56:04+00:00 2026-06-01T20:56:04+00:00

I have an SQL file which I want to parse and execute in oracle

  • 0

I have an SQL file which I want to parse and execute in oracle using cx_Oracle python library. The SQL file contains both classic DML/DDL and PL/SQL, eg. it can look like this:

create.sql:

-- This is some ; malicious comment
CREATE TABLE FOO(id numeric);

BEGIN
  INSERT INTO FOO VALUES(1);
  INSERT INTO FOO VALUES(2);
  INSERT INTO FOO VALUES(3);
END;
/
CREATE TABLE BAR(id numeric);

if I use this file in SQLDeveloper or SQL*Plus, it will be split into 3 queries and executed.

However, cx_Oracle.connect(…).cursor().execute(…) can take only ONE query at a time, not an entire file. I cannot simply split the string using string.split(';') (as suggested here execute a sql script file from cx_oracle? ), because both the comment will be split (and will cause an error) and the PL/SQL block will not be executed as single command, thus causing an error.

On the Oracle forum ( https://forums.oracle.com/forums/thread.jspa?threadID=841025 ) I’ve found that cx_Oracle itself does not support such thing as parse entire file. My question is — is there a tool to do this for me? Eg. a python library I can call to split my file into queries?

Edit: The best solutions seems to use SQL*Plus directly. I’ve used this code:

# open the file
f = open(file_path, 'r')
data = f.read()
f.close()

# add EXIT at the end so that SQL*Plus ends (there is no --no-interactive :(
data = "%s\n\nEXIT" % data

# write result to a temp file (required, SQL*Plus takes a file name argument)
f = open('tmp.file', 'w')
f.write(data)
f.close()

# execute SQL*Plus
output = subprocess.check_output(['sqlplus', '%s/%s@%s' % (db_user, db_password, db_address), '@', 'tmp.file'])

# if an error was found in the result, raise an Exception
if output.find('ERROR at line') != -1:
    raise Exception('%s\n\nStack:%s' % ('ERROR found in SQLPlus result', output))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:56:07+00:00Added an answer on June 1, 2026 at 8:56 pm

    It’s possible to execute multiple statements at the same time but it’s semi-hacky. You need to wrap your statements and execute them one at a time.

    >>> import cx_Oracle
    >>>
    >>> a = cx_Oracle.connect('schema/pw@db')
    >>> curs = a.cursor()
    >>> SQL = (("""create table tmp_test ( a date )"""),
    ... ("""insert into tmp_test values ( sysdate )""")
    ... )
    >>> for i in SQL:
    ...     print i
    ...
    create table tmp_test ( a date )
    insert into tmp_test values ( sysdate )
    >>> for i in SQL:
    ...     curs.execute(i)
    ...
    >>> a.commit()
    >>>
    

    As you’ve noted this doesn’t solve the semi-colon problem, for which there is no easy answer. As I see it you have 3 options:

    1. Write an over-complicated parser, which I don’t think is a good option at all.

    2. Do not execute SQL scripts from Python; have the code in either separate SQL scripts so the parsing is easy, in a separate Python file, embedded in your Python code, in a procedure in the database… etc. This is probably my preferred option.

    3. Use subprocess and call the script that way. This is the simplest and quickest option but doesn’t use cx_Oracle at all.

      >>> import subprocess
      >>> cmdline = ['sqlplus','schema/pw@db','@','tmp_test.sql']
      >>> subprocess.call(cmdline)
      
      SQL*Plus: Release 9.2.0.1.0 - Production on Fri Apr 13 09:40:41 2012
      
      Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.
      
      
      Connected to:
      Oracle Database 11g Release 11.2.0.1.0 - 64bit Production
      
      SQL>
      SQL> CREATE TABLE FOO(id number);
      
      Table created.
      
      SQL>
      SQL> BEGIN
        2    INSERT INTO FOO VALUES(1);
        3    INSERT INTO FOO VALUES(2);
        4    INSERT INTO FOO VALUES(3);
        5  END;
        6  /
      
      PL/SQL procedure successfully completed.
      
      SQL> CREATE TABLE BAR(id number);
      
      Table created.
      
      SQL>
      SQL> quit
      Disconnected from Oracle Database 11g Release 11.2.0.1.0 - 64bit Production
      0
      >>>
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file which contains sql statements. Now in this file, I want
I have a csv format file, which I want to import to sql server
I have an .sql file which contains some data like first_name, last_name, email etc.
I have a mdf file which is SQL Server 2008 file. I want to
I have a .sql file, which is a bunch of oracle pl/sql commands and
I have a rather large SQL file which starts with the byte order marker
I have created a batch file which automatically copy a .sql file to the
I have a SqlDump.sql file that works just fine when I apply it using
I have an SQL file which will give me an output like below: 10|1
I have a xml file on which I want to make a query to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.