使用 Pypyodbc 插入带单引号的格式化字符串
Inserting formatted string with single quotes with Pypyodbc
我正在尝试使用 Pypyodbc 将网络路径作为字符串值插入:
def insert_new_result(self, low, medium, high, reportpath):
reportpath = "'" + reportpath + "'"
self.insertsql = (
"""INSERT INTO [dbo].[Results] ([low],[medium], [high], [date]) VALUES
({low},{medium},{high}, GETDATE(),{reportpath})"""
.format(low=low, medium=medium, high=high, reportpath=reportpath))
self.conection.cursor().execute(self.insertsql).commit()
这是对
的评估
'INSERT INTO [dbo].[Results] ([low],[medium], [high], [date]) VALUES
(0,2,2, GETDATE(),\'\\share\dev-temp\report_10b29ef6-7436-11e6-ab96-534e57000000.html\')'
注意共享路径开头的额外单引号,导致无效 sql 错误。我已经尝试了一些类似 .format() 的东西,构建字符串并转义但是它一直包含单引号 after 第一个 \\。
如何让 self.insertsql 计算为
'INSERT INTO [dbo].[Results]
([low],[medium], [high], [date])
VALUES
(0,2,2, GETDATE(),'\\share\dev-temp\report_10b29ef6-7436-11e6-ab96-534e57000000.html\')'
如果您正在使用 string.format()
,那么您正在创建 动态 SQL,这会让您对 [= 的所有侮辱敞开心扉30=] 注入,就像搞乱字符串定界和转义。您应该简单地使用 参数化查询 ,像这样:
self.insertsql = """\
INSERT INTO [dbo].[Results] ([low],[medium], [high], [date], [reportpath])
VALUES (?, ?, ?, GETDATE(), ?)
"""
self.params = (low, medium, high, reportpath, )
self.conection.cursor().execute(self.insertsql, self.params).commit()
编辑回复:评论
作为简化的独立测试,代码
conn = pypyodbc.connect(conn_str, autocommit=True)
crsr = conn.cursor()
sql = """\
CREATE TABLE #Results
(
id INT IDENTITY PRIMARY KEY,
reportpath NVARCHAR(255)
)
"""
crsr.execute(sql)
# test data
reportpath = r"\share\dev-temp\report_10b29ef6-7436-11e6-ab96-534e57000000.html"
sql = "INSERT INTO #Results (reportpath) VALUES (?)"
params = (reportpath, )
crsr.execute(sql, params)
crsr.execute("SELECT * FROM #Results")
row = crsr.fetchone()
print(row)
显示这个
(1, u'\\share\dev-temp\report_10b29ef6-7436-11e6-ab96-534e57000000.html')
我正在尝试使用 Pypyodbc 将网络路径作为字符串值插入:
def insert_new_result(self, low, medium, high, reportpath):
reportpath = "'" + reportpath + "'"
self.insertsql = (
"""INSERT INTO [dbo].[Results] ([low],[medium], [high], [date]) VALUES
({low},{medium},{high}, GETDATE(),{reportpath})"""
.format(low=low, medium=medium, high=high, reportpath=reportpath))
self.conection.cursor().execute(self.insertsql).commit()
这是对
的评估'INSERT INTO [dbo].[Results] ([low],[medium], [high], [date]) VALUES
(0,2,2, GETDATE(),\'\\share\dev-temp\report_10b29ef6-7436-11e6-ab96-534e57000000.html\')'
注意共享路径开头的额外单引号,导致无效 sql 错误。我已经尝试了一些类似 .format() 的东西,构建字符串并转义但是它一直包含单引号 after 第一个 \\。
如何让 self.insertsql 计算为
'INSERT INTO [dbo].[Results]
([low],[medium], [high], [date])
VALUES
(0,2,2, GETDATE(),'\\share\dev-temp\report_10b29ef6-7436-11e6-ab96-534e57000000.html\')'
如果您正在使用 string.format()
,那么您正在创建 动态 SQL,这会让您对 [= 的所有侮辱敞开心扉30=] 注入,就像搞乱字符串定界和转义。您应该简单地使用 参数化查询 ,像这样:
self.insertsql = """\
INSERT INTO [dbo].[Results] ([low],[medium], [high], [date], [reportpath])
VALUES (?, ?, ?, GETDATE(), ?)
"""
self.params = (low, medium, high, reportpath, )
self.conection.cursor().execute(self.insertsql, self.params).commit()
编辑回复:评论
作为简化的独立测试,代码
conn = pypyodbc.connect(conn_str, autocommit=True)
crsr = conn.cursor()
sql = """\
CREATE TABLE #Results
(
id INT IDENTITY PRIMARY KEY,
reportpath NVARCHAR(255)
)
"""
crsr.execute(sql)
# test data
reportpath = r"\share\dev-temp\report_10b29ef6-7436-11e6-ab96-534e57000000.html"
sql = "INSERT INTO #Results (reportpath) VALUES (?)"
params = (reportpath, )
crsr.execute(sql, params)
crsr.execute("SELECT * FROM #Results")
row = crsr.fetchone()
print(row)
显示这个
(1, u'\\share\dev-temp\report_10b29ef6-7436-11e6-ab96-534e57000000.html')