0

I have a Python script that I run in PyScripter (Windows) that I use to execute SQL on an Oracle database. The SQL creates a view.

import sys
import arcpy
arcpy.env.workspace = sys.path[0]
egdb_conn = arcpy.ArcSDESQLExecute(r"Database Connections\Connection1.sde")
sql_statement = """

CREATE OR REPLACE VIEW ROAD_VW AS
SELECT
    NAME
    ,FROM_
    ,TO_
FROM
    USER1.ROAD

"""
egdb_return = egdb_conn.execute(sql_statement)
print "Complete."

The script successfully creates a view in the database.

The problem is that the line breaks in the database view definition only seem to work (be visible) in certain programs, not others. If I access the view definition (in say - MS Access or ArcGIS Desktop), copy it, then paste it, this happens:

Line breaks are visible in:

SELECT
    NAME
    ,FROM_
    ,TO_
FROM
    USER1.ROAD
  1. Stack Overflow
  2. Microsoft Word
  3. PyScripter

Line breaks are not visible in:

SELECT        NAME        ,FROM_        ,TO_    FROM        USER1.ROAD
  1. Notepad
  2. MS Access >> SQL editor
  3. ArcGIS Desktop >> Create View

Can I format the SQL text so that, when it's executed to create a view, the view definition's line breaks are preserved (when the SQL definition text is used in other programs)?

4
  • 1
    maybe you can have a look at this link and use that for new lines in your code. That is a suggestion and not a tested answer, MS Access or ArcGIS applications might be displaying them the way they want. Commented May 9, 2017 at 14:26
  • Is the Python running on Linux, i.e. are the line breaks in the Python just a line feed LF and Oracle requires a carriage return as well (CRLF). Commented May 9, 2017 at 14:41
  • @Ben I've updated the question: I have a Python script that I run in PyScripter (Windows) that I use to execute SQL on an Oracle database. The python is running on Windows. I don't have any details about the Oracle Server, but my uneducated guess is that it's irrelevant. Commented May 9, 2017 at 14:44
  • @Ben Thanks, I've answered my own question. Commented May 11, 2017 at 17:47

1 Answer 1

1

As hinted by @Sudipta Mondal, I need to convert \n to \r\n:

sql_statement = sql_statement.replace('\n', '\r\n')


Related resources:

Difference between \n and \r?

Replace \n with {something else}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.