sql='Update dbo.[T_Road] Set Longitude_Tile=\'%s\',Latitude_Tile=\'%s\',Polyline_Tile=\'%s\',Longitude_TD=\'%s\',Latitude_TD=\'%s\',Polyline_TD=\'%s\',Longitude_BD=\'%s\',Latitude_BD=\'%s\',Polyline_BD=\'%s\',Longitude_GD=\'%s\',Latitude_GD=\'%s\',Polyline_GD=\'%s\',RoadLength=\'%s\' From [dbo].[T_Road] Where Id = \'%s\';\n'%(row['Longitude_Tile'],row['Latitude_Tile'],row['Polyline_Tile'],row['Longitude_TD'],row['Latitude_TD'],row['Polyline_TD'],row['Longitude_BD'],row['Latitude_BD'],row['Polyline_BD'],row['Longitude_GD'],row['Latitude_GD'],row['Polyline_GD'],row['RoadLength'],row['Id'])怎么把这个sql语句改成参数化查询
时间: 2024-01-31 17:02:49 浏览: 30
要将该SQL语句改为参数化查询,您可以使用SQL Server提供的参数化查询功能。下面是将您提供的SQL语句改写为参数化查询的示例代码:
```python
sql = "UPDATE dbo.[T_Road] SET Longitude_Tile = @Longitude_Tile, Latitude_Tile = @Latitude_Tile, Polyline_Tile = @Polyline_Tile, Longitude_TD = @Longitude_TD, Latitude_TD = @Latitude_TD, Polyline_TD = @Polyline_TD, Longitude_BD = @Longitude_BD, Latitude_BD = @Latitude_BD, Polyline_BD = @Polyline_BD, Longitude_GD = @Longitude_GD, Latitude_GD = @Latitude_GD, Polyline_GD = @Polyline_GD, RoadLength = @RoadLength WHERE Id = @Id;"
# 创建连接和命令对象
conn = pyodbc.connect(connection_string)
cursor = conn.cursor()
# 执行查询
params = {
'Longitude_Tile': row['Longitude_Tile'],
'Latitude_Tile': row['Latitude_Tile'],
'Polyline_Tile': row['Polyline_Tile'],
'Longitude_TD': row['Longitude_TD'],
'Latitude_TD': row['Latitude_TD'],
'Polyline_TD': row['Polyline_TD'],
'Longitude_BD': row['Longitude_BD'],
'Latitude_BD': row['Latitude_BD'],
'Polyline_BD': row['Polyline_BD'],
'Longitude_GD': row['Longitude_GD'],
'Latitude_GD': row['Latitude_GD'],
'Polyline_GD': row['Polyline_GD'],
'RoadLength': row['RoadLength'],
'Id': row['Id']
}
cursor.execute(sql, params)
conn.commit()
# 关闭连接
cursor.close()
conn.close()
```
在上面的示例中,我们使用了参数化查询来替代原始的SQL语句中的值占位符。然后,我们传递一个参数字典 `params` 给 `execute()` 方法,其中包含了每个参数的名称和对应的值。这样可以确保参数值被正确地进行了转义和处理。
请注意,示例代码中的 `connection_string` 是连接到SQL Server数据库的连接字符串,您需要根据自己的环境进行相应的配置和替换。
通过使用参数化查询,可以提高代码的安全性,并且可以避免SQL注入攻击。同时,参数化查询还可以帮助数据库优化执行计划并提高性能。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)