gpt4 book ai didi

MySQL-python:SELECT 返回 'long' 而不是查询

转载 作者:行者123 更新时间:2023-11-29 01:29:57 24 4
gpt4 key购买 nike

我在使用 mysql-python 在已建立的数据库上运行选择查询时遇到问题。问题是返回了一个数字,Python 将其称为 long,而不是查询的数据 - 应该注意的是,这个数字对应于记录数这应该被返回(我登录到数据库并从 MySQL 运行查询以确保)。

代码如下:

db = MySQLdb.connect(db = 'testdb', user='testuser', passwd='test', host='localhost', charset='utf8', use_unicode=True)
dbc = db.cursor()
result = dbc.execute("""SELECT %s FROM example_movie""", ('title',))
urls = [row[0] for row in result]

最后一段代码,urls = [row[0] for row in result] 是将所有内容放入列表中。

错误看起来像这样:

 TypeError: 'long' object is not iterable

当我有 python print result 它返回:

('RESULT:', 52L)

当我像 str(result) 这样包含 result 时,它只返回数字 52(不是 long)

非常感谢任何帮助和建议!

最佳答案

dbc.execute 的返回值不是选择的结果;我相信这是结果中的行数。为了获得实际结果,您需要调用其中一种 fetch 方法。请参阅文档 here .

您应该更新您的代码以阅读:

db = MySQLdb.connect(db = 'testdb', user='testuser', passwd='test', host='localhost', charset='utf8', use_unicode=True)
dbc = db.cursor()
row_count = dbc.execute("""SELECT title FROM example_movie""")
results = dbc.fetchall()
urls = [row[0] for row in result]

关于MySQL-python:SELECT 返回 'long' 而不是查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18369512/

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