import pyodbc
con=pyodbc.connect('Driver={SQL Server};Server=New;Database=Countrydatabase;Trusted_connection=yes')
cur=con.cursor()
cur.execute("TRUNCATE Countrydatabase..region")
con.close()
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 pyodbc 模块截断表。但它不断抛出错误。
回溯(最近一次通话):文件“C:/Users/sean/Desktop/script.py”,第 6 行,在 cur.execute("TRUNCATE Countrydatabase..region") pyodbc.ProgrammingError: ('42000', " [42000] [Microsoft][ODBC SQL Server 驱动程序][SQL Server]'Countrydatabase' 附近的语法不正确。(102) (SQLExecDirectW)")
当我使用“SELECT”语句时,相同的代码工作正常。
-----------------------
country | city | ids
-----------------------
India Mumbai 1
India Chennai 2
India Kolkata 3
---------------------
USA New York 2
USA Utah 3
---------------------
Run Code Online (Sandbox Code Playgroud)
我从表中给出了一个样本.从表中,我试图查询所有没有id 1的国家.我写了这个(国家没有包含在Where条件中,因为它需要适用于表中的所有国家).
Select * from Countries
WHERE id<>1
Run Code Online (Sandbox Code Playgroud)
我懂了.
-----------------------
country | city | ids
-----------------------
India Chennai 2
India Kolkata 3
---------------------
USA New York 2
USA Utah 3
---------------------
Run Code Online (Sandbox Code Playgroud)
但我需要输出只包含USA(没有id = 1).这有什么解决方法吗?