mysql 元组_Python mysql连接器返回元组
发布日期:2021-06-24 17:12:33 浏览次数:2 分类:技术文章

本文共 975 字,大约阅读时间需要 3 分钟。

我通过

mysql连接器连接到

mysql数据库并运行一个简单的查询来提取ID列表.我需要遍历该列表并将它们传递给其他代码.出于某种原因,我得到了一个元组列表.这是预期的行为吗?如果没有,我做错了什么?

这是我的代码的片段:

import mysql.connector

conn = mysql.connector.connect(host='127.0.0.1', database='t', user='r', password='pwd')

cursor = conn.cursor()

query = ( "select id from T where updated < '%s'" % (run_date) )

cursor.execute(query)

for row in cursor:

print (row)

cursor.close()

我得到以下回复(来自d / b中的INT字段):

(Decimal('991837'),)

(Decimal('991838'),)

(Decimal('991839'),)

(Decimal('991871'),)

(Decimal('991879'),)

(Decimal('991899'),)

(Decimal('992051'),)

(Decimal('992299'),)

(Decimal('992309'),)

最佳答案 是的,这是预期的行为.将游标用作可迭代基本上等效于使用fetchone()方法对其进行循环.从

documentation for fetchone()(强调我的):

This method retrieves the next row of a query result set and returns a

single sequence, or None if no more rows are available. By default,

the returned tuple consists of data returned by the MySQL server,

converted to Python objects. If the cursor is a raw cursor, no such

conversion occurs;

转载地址:https://blog.csdn.net/weixin_34017915/article/details/114777788 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java画布中让整个图一起移动_Java-沿任何方向连续滚动一个大型无缝图像
下一篇:java从邮箱获取邮件_javaMail正确读取邮箱内容的方式

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月08日 17时48分09秒