【问题标题】:MySql, python, Visual Studio Code : No result set to fetch fromMySql、python、Visual Studio 代码:没有要从中获取的结果集
【发布时间】: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。

标签: python mysql


【解决方案1】:

对于这种特定情况,您不需要将连接设置为自动提交,因为您只是从表中读取。

  • 检查用户是否对表有 SELECT 权限

【讨论】:

  • 感谢您的回复。
  • 我找到了解决方法。我正在使用 Visual Studio Code 进行编辑和调试。如果 cursor.execute 和 cursor.fetchall 之间有断点,结果表会被刷新。如果这两个语句之间没有断点 -> 一切正常,结果表包含 3 行。
【解决方案2】:

试试这个:

cur = conn.cursor()

showdata = "SELECT * FROM testtable"

尝试:

 cur.execute(showdata)
 rows = cur.fetchall()
 print(rows)

除了:

 print ("Data not found!")

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    相关资源
    最近更新 更多