pythonsql语句换⾏_执⾏SQL的Python脚本:在创建视图时保
留SQL⽂本中的换。。。
执⾏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
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 = ute(sql_statement)
print "Complete."
该脚本在数据库中成功创建了⼀个视图。
问题是数据库视图定义中的换⾏符似乎只在某些程序中⼯作(可见),⽽不是其他程序。 如果我访问视图定义(例如 - MS Access或ArcGIS Desktop),复制它,然后粘贴它,会发⽣这种情况:
换⾏符在以下位置可见:
SELECT
NAME
,FROM_
,TO_
FROM
USER1.ROAD
堆栈溢出
微软Word
PyScripter
换⾏符在以下位置不可见:
sql语句替换表中内容
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
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 = ute(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)?
更新时间:2020-02-19 00:21
最满意答案
正如@Sudipta Mondal暗⽰的那样,我需要将\n转换为\r\n :
sql_statement = place('\n', '\r\n')
相关资源:
As hinted by @Sudipta Mondal, I need to convert \n to \r\n:
sql_statement = place('\n', '\r\n')
Related resources:
2017-05-23
相关问答
最简单的解决⽅案是使⽤下⾯的CSS属性简单地设置插⼊⽂本的元素的样式: white-space: pre-wrap;
这个属性使匹配元素中的空⽩和换⾏符像⼀样被处理。 也就是说,连续的空⽩不会折叠,并且线条在显式换⾏处被破坏(但如果它们超出元素的宽度,也会⾃动换⾏)。 鉴于迄今发布的⼏个答案都容易受到HTML注⼊ (例如,因为它们将未转义的⽤户输⼊分配给innerHTML )或其他错误,请让我举例说明
如何基于您的原始代码安全正确地执⾏此操作: Elem
...
保存为扩展名为.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
...
我想在呈现该⽇期之前,您应该使⽤place(“;”,Environment.NewLine); 并返回新的IHTMLString(yourcontent)或使⽤HTML.Raw(yourcontent) 这是伪代码,只是让你明⽩。 I think before rendering that date you should use
...
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 = place('\n', '\r\n') 相关资源: \ n和\ r \ n之间的区别? 将\ n替换为{something else} As hinted by @Sudipta Mondal, I need to convert \n to \r\n: sql_statement = place('\n', '\r\n')
...

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。