这个命令工作正常..
count = conn.cursor().execute("select COUNT(*) FROM Orders;").fetchall()
但是,此命令会返回错误
count = conn.cursor().execute("select COUNT(*) FROM "Summary of Sales"; ").fetchall()
错误是..
Traceback (most recent call last):
File "python-database.py", line 39, in <module>
print(row_count())
File "python-database.py", line 29, in row_count
count = conn.cursor().execute(query).fetchall()
File "/usr/local/lib/python2.7/dist-packages/pypyodbc.py", line 1595, in execute
self.execdirect(query_string)
File "/usr/local/lib/python2.7/dist-packages/pypyodbc.py", line 1621, in execdirect
check_success(self, ret)
File "/usr/local/lib/python2.7/dist-packages/pypyodbc.py", line 985, in check_success
ctrl_err(SQL_HANDLE_STMT, ODBC_obj.stmt_h, ret, ODBC_obj.ansi)
File "/usr/local/lib/python2.7/dist-packages/pypyodbc.py", line 953, in ctrl_err
raise ProgrammingError(state,err_text)
pypyodbc.ProgrammingError: (u'42000', u"[42000] [FreeTDS][SQL Server]Incorrect syntax near 'Summary of Sales'.")
我使用 SQL 分析器来查看确切的 sql 查询,它非常完美,我什至已经将其处理并粘贴到与数据库的控制台会话中进行测试,它返回行数没有问题。我尝试了所有不同的单引号和双引号组合。似乎如果放置一个没有任何引号的单个单词的表名,它就可以正常工作。如果我尝试输入一个包含多个单词的表名,我必须使用引号,但它会失败。
希望你们能对此有所了解,它阻止了我的项目,:(