在运行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)")
答案 0 :(得分:0)
您不能选择*,也不能指定列名,因此将SQL字符串重写为:
SELECT [StreetAddressLine1],[City],[ZipCode] FROM Dim.Vet
最佳实践:始终尝试从SQL界面尝试您的SQL命令,直到它们起作用为止,然后将它们引入您的代码中并进行调整。