【发布时间】:2018-05-31 12:23:58
【问题描述】:
我正在尝试从表中获取行,但尽管表中有 3 行,但总是收到“无”。 我在这个论坛上多次阅读了相同的问题,但没有一个解决方案对我有用,到目前为止,我尝试设置自动提交、显式提交、缓冲游标、非缓冲游标、fetchall、fetchone。 Select 语句在 MySl 工作台中运行良好,在 MySql 工作台中返回 3 行,
这是我的设置:
Win32 上的 Python 3.6.5(v3.6.5:f59c0932b4,2018 年 3 月 28 日,17:00:18)[MSC v.1900 64 位(AMD64)]
MySql 8.0.11 社区服务器 - GPL Win64 (x86_64)
适用于 Python 3.6.5 的 MySql 连接器
conn = mysql.connector.connect(database='test',
user='usr',
password='pwd',
host='localhost',
port=3306)
# autocommit -> advice from web
conn.autocommit = True
print(conn.autocommit)
cursor = conn.cursor()
cursor.execute("SELECT * FROM testtable")
rows = cursor.fetchall()
# .... -> program stops here. please see output below
res = len(rows)
print(res)
print(rows)
cursor.close()
conn.close()
输出:
True Traceback (most recent call last): File "d:\PyDb\dbtest02_2.py", line 16, in <module> rows = cursor.fetchall() File "C:\Python\Python365\lib\site-packages\mysql\connector\cursor_cext.py", line 488, in fetchall raise errors.InterfaceError("No result set to fetch from.") mysql.connector.errors.InterfaceError: No result set to fetch from.
非常感谢您的任何建议。
【问题讨论】:
-
奇怪:我刚刚发现它可能与我用作编辑器的 MS Visual Studio Code 有关。在我看来,如果我在没有断点的情况下调试脚本,那么有时(?)会起作用。如果我使用一些断点逐步调试,则 fetch 将返回 NONE。