mysql unread result found,Python mysql.connector InternalError:关闭游标时发现未读结果

I want to read part of result from cursor and then close it without reading all result. cursor.close() raises InternalError: Unread result found. Is it possible to close cursor without iterating through all result or using buffer option?

Update:

My query get about 3000 records, I aim to getting first several records which fit some conditions. After iterating through part of result, I get what I want. Then I want to just abandon unread result. I don't use buffer option which, as I know, will read all result immediately. This question is Not duplicate of Python MySQL connector - unread result found when using fetchone

def chooseInstrumentsFromOrigin(self, time):

sql = """select symbol, name, total_ratio, outstanding_ratio from market_values

where time = %s order by {captype} asc""".format(captype=self.strategy_data['captype'])

args = [time]

conn = mysql.connector.connect(**mysql_config)

cursor = conn.cursor(dictionary=True)

cursor.execute(sql, args)

# This function will return half way.

symbols = self.chooseInstrumentsFromLeaders(time, cursor)

# I don't want this line!

for i in cursor: pass

cursor.close()

conn.close()

return symbols

解决方案

It would appear that you need:

cursor = conn.cursor(buffered=True,dictionary=true)

in order to abandon a resultset mid-stream.

Full disclosure, I am a mysql dev, not a python dev.

All rows are read immediately, true. Fantasic for small to mid-sized resultsets.

The latter reference above states:

For queries executed using a buffered cursor, row-fetching methods

such as fetchone() return rows from the set of buffered rows. For

nonbuffered cursors, rows are not fetched from the server until a

row-fetching method is called. In this case, you must be sure to fetch

all rows of the result set before executing any other statements on

the same connection, or an InternalError (Unread result found)

exception will be raised.

As a side note, you can modify your strategy by using pagination. The MySQL LIMIT clause supports this with the offset,pageSize settings:

[LIMIT {[offset,] row_count | row_count OFFSET offset}]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值