gpt4 book ai didi

python - 如何在 Python 的 PyPyODBC 模块中使用 FetchAll?

转载 作者:行者123 更新时间:2023-11-28 21:56:00 25 4
gpt4 key购买 nike

我正在尝试使用 PyPyODBC ,特别是对此 example 的看法.但是,我对创建新表不感兴趣,我只想读取数据(驻留在 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 中调查数据。如何做到这一点?

提前谢谢你。

最佳答案

删除 cur.commit() 行,SELECT 不需要它。

我能够使用 pyodbc 重现错误,所以我假设同样适用于 pypyodbc。您应该能够应用 pyodbc examples对于您的代码,我认为它们比 pypyodbc 当前的产品更完整。

关于python - 如何在 Python 的 PyPyODBC 模块中使用 FetchAll?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22180362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com