我正在尝试使用PyPyODBC,特别是在这个例子中。但是,我对创建新表不感兴趣,我只想读取数据(位于 SQL Server 的数据库/表中)并在 Python 中使用它。
以下代码可以正常工作,但以下部分 (
for row in cur.fetchall():
) 除外。
from __future__ import print_function
import pypyodbc
from pandas import *
import pandas as pd
con = pypyodbc.connect(driver = '{SQL Server}', server = 'WIN-SAV35R5AKCQ',database ='V2.0.6_X12_837I')
cur = con.cursor()
cur.execute('''SELECT * FROM TS837_2400;''')
cur.commit()
for d in cur.description:
print (d[0], end=" ")
print('')
for row in cur.fetchall():
for field in row:
print (field, end=" ")
print ('')
cur.close()
con.close()
但是,我收到此错误。
---------------------------------------------------------------------------
Error Traceback (most recent call last)
<ipython-input-70-c08d5a03056a> in <module>()
14 print('')
15
---> 16 for row in cur.fetchall():
17 for field in row:
18 print (field, end=" ")
C:\Users\Administrator\Anaconda\lib\site-packages\pypyodbc.pyc in fetchall(self)
1805 rows = []
1806 while True:
-> 1807 row = self.fetchone()
1808 if row is None:
1809 break
C:\Users\Administrator\Anaconda\lib\site-packages\pypyodbc.pyc in fetchone(self)
1900 return None
1901 else:
-> 1902 check_success(self, ret)
1903
1904 def __next__(self):
C:\Users\Administrator\Anaconda\lib\site-packages\pypyodbc.pyc in check_success(ODBC_obj, ret)
981 if ret not in (SQL_SUCCESS, SQL_SUCCESS_WITH_INFO, SQL_NO_DATA):
982 if isinstance(ODBC_obj, Cursor):
--> 983 ctrl_err(SQL_HANDLE_STMT, ODBC_obj.stmt_h, ret, ODBC_obj.ansi)
984 elif isinstance(ODBC_obj, Connection):
985 ctrl_err(SQL_HANDLE_DBC, ODBC_obj.dbc_h, ret, ODBC_obj.ansi)
C:\Users\Administrator\Anaconda\lib\site-packages\pypyodbc.pyc in ctrl_err(ht, h, val_ret, ansi)
959 raise OperationalError(state,err_text)
960 elif state[:2] in (raw_s('IM'),raw_s('HY')):
--> 961 raise Error(state,err_text)
962 else:
963 raise DatabaseError(state,err_text)
Error: (u'HY010', u'[HY010] [Microsoft][ODBC Driver Manager] Function sequence error')
问题: 最终,我希望能够在 Python 中调查数据。如何做到这一点?
先感谢您。