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)?

User1974
  • 276
  • 1
  • 17
  • 63
  • 1
    maybe you can have a look at this [link](http://stackoverflow.com/a/1761064/1013142) 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. – Sudipta Mondal May 09 '17 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). – Ben May 09 '17 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. – User1974 May 09 '17 at 14:44
  • @Ben Thanks, I've answered my own question. – User1974 May 11 '17 at 17:47

1 Answers1

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}

Community
  • 1
  • 1
User1974
  • 276
  • 1
  • 17
  • 63