为什么Python数据库查询会以这种方式返回记录集结果?
我四处寻找答案,以了解为什么查询结果以这种格式返回以及如何转换为列表。
data =cursor.fetchall()
当我打印数据时,结果是: (('car',), ('boat',), ('plane',), ('truck',))
我希望将结果显示为 ["car", "boat", "plane “, “卡车”]
I have searched high and low for an answer to why query results returned in this format and how to convert to a list.
data = cursor.fetchall()
When I print data, it results in:
(('car',), ('boat',), ('plane',), ('truck',))
I want to have the results in a list as ["car", "boat", "plane", "truck"]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它以这种方式返回它,因为记录集由许多数据行组成,而不是单个元素的列表。
如果您想使用列表理解,您可以将其展平:
It's returning it in that way because a recordset is comprised of many rows of data, not a list of single elements.
You can flatten it if you want using a list comprehension:
fetchall() 的结果返回一个行数组,其中每一行都是一个每列一个值的数组。
即使您只选择一列,您仍然会得到一组数组,但每一行只有一个值。
The result for fetchall() returns an array of rows, where each row is an array with one value per column.
Even if you are selecting only one column, you will still get an array of arrays, but only one value for each row.