mysql 元组_Python mysql连接器返回元组

我通过

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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值