Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,当向SQL Server插入数据时,如何转义特殊字符?_Python_Sql Server - Fatal编程技术网

在python中,当向SQL Server插入数据时,如何转义特殊字符?

在python中,当向SQL Server插入数据时,如何转义特殊字符?,python,sql-server,Python,Sql Server,我想在SQL Server中插入一个html文件,但是文档中有一些特殊的字符,我怎样才能转义这些特殊的字符呢 下面是我的代码: import pymssql import os txt_file = u'E:/project/robot_framework/xxxx/logs/log-20170318-143134.html' file_name = os.path.split(txt_file)[1] conn = pymssql.connect(host='192.168.0.888',us

我想在SQL Server中插入一个html文件,但是文档中有一些特殊的字符,我怎样才能转义这些特殊的字符呢

下面是我的代码:

import pymssql
import os
txt_file = u'E:/project/robot_framework/xxxx/logs/log-20170318-143134.html'
file_name = os.path.split(txt_file)[1]
conn = pymssql.connect(host='192.168.0.888',user='sa',password='xxxxx',database='Automation')
content = open(txt_file, 'r')
cur = conn.cursor()
sql = "insert into log_file (File_Name,RunID,File_VARCHAR_Content) values('%s',1,'%s')" % (file_name, content.readlines())
cur.execute(sql)
cur.close()
conn.close()
但我犯了错误:

回溯(最近一次调用):文件“E:\project\robot\u framework\PythonDemo\src\try.py”,第20行,在 pymssql.Cursor.execute(pymssql.c:7491)pymssql.ProgrammingError中第464行的cur.execute(sql)文件“pymssql.pyx”:
(102,“不正确的语法靠近”您可以使用python正则表达式库“re”


您可以使用python正则表达式库“re”


因为这是我在startpage上的第一个搜索结果:

如果您不知道此代码的作用,请不要更改此代码

conn = pymssql.connect(host='xxx.xxx.xxx.xxx',user='xxxxxxx',password='xxxxx',database='Whatever')
c=conn.cursor()
c.execute("SELECT id,KartenUid FROM Benutzer WHERE KartenUid=%s;",("b99cec01"))
row=c.fetchone()
#row=(UUID('6533f7d3-f190-45fd-95e0-72e3add0e9db'), 'b99cec01')
c.execute("SELECT id,KartenUid FROM Benutzer WHERE KartenUid=%s;",("b99cec01; DROP TABLE Benutzer;"))
row=c.fetchone()
#row=None
c.execute("SELECT id,KartenUid FROM Benutzer WHERE KartenUid=%s;",("b99cec01"))
row=c.fetchone()
#row=(UUID('6533f7d3-f190-45fd-95e0-72e3add0e9db'), 'b99cec01')

因此,为了防止SQL注入(和其他转义问题),请使用
pymssql.Cursor.execute(SQL,paramstuple)
方法(请参阅)。

因为这是我在startpage上的第一个搜索结果:

如果您不知道此代码的作用,请不要更改此代码

conn = pymssql.connect(host='xxx.xxx.xxx.xxx',user='xxxxxxx',password='xxxxx',database='Whatever')
c=conn.cursor()
c.execute("SELECT id,KartenUid FROM Benutzer WHERE KartenUid=%s;",("b99cec01"))
row=c.fetchone()
#row=(UUID('6533f7d3-f190-45fd-95e0-72e3add0e9db'), 'b99cec01')
c.execute("SELECT id,KartenUid FROM Benutzer WHERE KartenUid=%s;",("b99cec01; DROP TABLE Benutzer;"))
row=c.fetchone()
#row=None
c.execute("SELECT id,KartenUid FROM Benutzer WHERE KartenUid=%s;",("b99cec01"))
row=c.fetchone()
#row=(UUID('6533f7d3-f190-45fd-95e0-72e3add0e9db'), 'b99cec01')

因此,为了防止SQL注入(和其他转义问题),请使用
pymssql.Cursor.execute(SQL,paramstuple)
方法(请参阅).

Hey Mustafa,我创建了这个转义字符串,但是我仍然无法将这个字符串插入我的microsoft sql server…有什么线索可以解释为什么会发生这种情况吗?因为
re.escape
会转义正则表达式中的特殊字符,而不是sql语句。他的问题是txt文件中的特殊字符,所以他不需要转义sql语句用于解决方案。嘿,Mustafa,我创建了这个转义字符串,但是我仍然无法将这个字符串插入我的microsoft sql server…有什么线索可以解释为什么会发生这种情况吗?因为
re.escape
转义与正则表达式有关的特殊字符,而不是sql语句。他的问题是txt文件中的特殊字符,所以他不需要为了解决这个问题而转义sql语句。