当我尝试通过pyodbc连接时,为什么会收到此ProgrammingError?

时间:2019-08-14 01:25:58

标签: python error-handling pyodbc

在运行cript以连接到odbc之后,我收到一个编程错误:为什么会发生这种情况?

conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=SSIV311\ION;'
                      'Database=Warehouse;'
                      'Trusted_Connection=yes;')

cursor = conn.cursor()
cursor.execute('SELECT * [StreetAddressLine1],[City],[ZipCode] FROM Dim.Vet')


  File "<ipython-input-54-83008ec40275>", line 1, in <module>
    cursor.execute('SELECT * [StreetAddressLine1],[City],[ZipCode] FROM Dim.Vet')

ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'StreetAddressLine1'. (102) (SQLExecDirectW)")

1 个答案:

答案 0 :(得分:0)

您不能选择*,也不能指定列名,因此将SQL字符串重写为:

SELECT [StreetAddressLine1],[City],[ZipCode] FROM Dim.Vet

最佳实践:始终尝试从SQL界面尝试您的SQL命令,直到它们起作用为止,然后将它们引入您的代码中并进行调整。