[英]How to DROP TABLE with python odbc on SQL Server?
I am using the ODBC Driver 17 for SQL Server and pyodbc. 我正在使用用于SQL Server和pyodbc的ODBC驱动程序17。 The following code does not work.
以下代码不起作用。
cursor.execute("""DROP TABLE IF EXISTS ?""", "table_name")
What is the problem? 问题是什么?
One possibility might be to use an anonymous code block that employs the QUOTENAME function to ensure that the string you pass is treated as a complete table name: 一种可能是使用使用QUOTENAME函数的匿名代码块,以确保将您传递的字符串视为完整的表名:
# this table name could be problematic if QUOTENAME wasn't used
tbl_name = "]; TRUNCATE TABLE Customer; --"
sql = """\
SET NOCOUNT ON;
DECLARE @drop NVARCHAR(max);
SET @drop = 'DROP TABLE IF EXISTS ' + QUOTENAME(?);
EXEC sp_executesql @drop;
"""
crsr.execute(sql, tbl_name)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.