python sql语句换行_执行SQL的Python脚本:在创建视图时保留SQL文本中的换行符(Python script that executes SQL: Preserve line brea...

执行SQL的Python脚本:在创建视图时保留SQL文本中的换行符(Python script that executes SQL: Preserve line breaks in SQL text when creating a view)

我有一个Python脚本 ,我在PyScripter(Windows)中运行,用于在Oracle数据库上执行SQL。 SQL创建一个视图。

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."

该脚本在数据库中成功创建了一个视图。

问题是数据库视图定义中的换行符似乎只在某些程序中工作(可见),而不是其他程序。 如果我访问视图定义(例如 - MS Access或ArcGIS Desktop),复制它,然后粘贴它,会发生这种情况:

换行符在以下位置可见:

SELECT

NAME

,FROM_

,TO_

FROM

USER1.ROAD

堆栈溢出

微软Word

PyScripter

换行符在以下位置不可见:

SELECT NAME ,FROM_ ,TO_ FROM USER1.ROAD

记事本

MS Access >> SQL编辑器

ArcGIS Desktop >>创建视图

我可以格式化SQL文本,以便在执行创建视图时,保留视图定义的换行符(当SQL定义文本用于其他程序时)?

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

Stack Overflow

Microsoft Word

PyScripter

Line breaks are not visible in:

SELECT NAME ,FROM_ ,TO_ FROM USER1.ROAD

Notepad

MS Access >> SQL editor

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

原文:https://stackoverflow.com/questions/43872270

更新时间:2020-02-19 00:21

最满意答案

正如@Sudipta Mondal暗示的那样,我需要将\n转换为\r\n :

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

相关资源:

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

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

Related resources:

2017-05-23

相关问答

最简单的解决方案是使用下面的CSS属性简单地设置插入文本的元素的样式: white-space: pre-wrap;

这个属性使匹配元素中的空白和换行符像一样被处理。 也就是说,连续的空白不会折叠,并且线条在显式换行处被破坏(但如果它们超出元素的宽度,也会自动换行)。 鉴于迄今发布的几个答案都容易受到HTML注入 (例如,因为它们将未转义的用户输入分配给innerHTML )或其他错误,请让我举例说明如何基于您的原始代码安全正确地执行此操作: document.getElem

...

保存为扩展名为.sql的文件。 然后使用SQLPLus等SQL连接工具从命令行运行(您没有指明您所在的数据库) save as a file with .sql extension. then run from the command line with a sql connection tool like SQLPLus (you don't indicate which database you are on)

如果使用print输出文本,请在语句末尾添加逗号以删除换行符。 例如 print 'Some Text',

可能是您的打印文本中已有新行,并且不需要从打印中添加额外的行。 如果没有,请尝试在字符串上使用.rstrip('\n')删除任何换行符。 If you are using print to output the text, append a comma at the end of your statement to remove the new-line character. e.g. p

...

我想在呈现该日期之前,您应该使用yourcontent.replace(“;”,Environment.NewLine); 并返回新的IHTMLString(yourcontent)或使用HTML.Raw(yourcontent) 这是伪代码,只是让你明白。 I think before rendering that date you should use yourcontent.replace(";", Environment.NewLine); and return new IHTMLStri

...

stripslashes绝对不是输出到html的方式,以逃避不受信任的输入,使用htmlspecialchars 。 最好的方法是利用white-space css规则:

如果在输出中看到额外的斜杠,则数据库中的数据错误。 修复,而不是输出。 stripslashes is definately not the way to output

...

正如回溯中所述,您的SQL语法中有错误。 表名是错误的。 从SQL手册 - 标识符可以以数字开头,但除非报价不能只包含数字。 As mentioned in the traceback, you have an error in your SQL syntax. Table name is wrong. From the SQL manual - Identifiers may begin with a digit but unless quoted may not consist solely

...

您可以删除回车符( char(13)或\r )并保留换行符( char(10)或\n )。 You can remove the carriage returns (char(13) or \r) and keep the line feeds (char(10) or \n).

删除SQL脚本中的换行符后,它工作。 因此,目前SQL脚本中没有换行符,除非它们用作语句分隔符。 After removing the line breaks within the sql script, it worked. So currently no line breaks in SQL-Scripts, except they are used as statement delimiters.

mysql_real_escape_string不会删除换行符,它会逃脱它们。 在存储字符串时应该可以正常工作,并在输出数据时应用nl2br (可能与htmlspecialchars()结合使用以防止用户输入原始HTML)。 这是最好的方式。 mysql_real_escape_string doesn't remove line breaks, it escapes them. It should work okay to escape the string when storing it, a

...

正如@Sudipta Mondal暗示的那样,我需要将\n转换为\r\n : sql_statement = sql_statement.replace('\n', '\r\n') 相关资源: \ n和\ r \ n之间的区别? 将\ n替换为{something else} As hinted by @Sudipta Mondal, I need to convert \n to \r\n: sql_statement = sql_statement.replace('\n', '\r\n')

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值