tornado mysql:如何获得curs的大小

2024-04-25 05:07:09 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是tornado如何使用MySQL:

from __future__ import print_function
from tornado import ioloop, gen
import tornado_mysql


@gen.coroutine
def f():
    conn = yield tornado_mysql.connect(host='127.0.0.1', port=3306, user='user', passwd='passwd', db='mydb')
    cur = conn.cursor()
    yield cur.execute(query)   
    cur.close()
    conn.close()
    return cur

query = "select id from tablename"
for row in ioloop.IOLoop.current().run_sync(f):
    print(row)

如果print(row)执行零时间,则意味着select id from tablename什么也得不到。

我只想知道如何立即获得ioloop.IOLoop.current().run_sync(f)的大小(类似于ioloop.IOLoop.current().run_sync(f).size == 0),以便确定某个sql查询是否获得了某些内容。你知道吗


Tags: runfromimportmysqlsynccurrentconntornado
1条回答
网友
1楼 · 发布于 2024-04-25 05:07:09

在一个python序列上反复迭代一次,或者不迭代就知道序列中有多少东西是不可能的。为了安全起见,必须将结果复制到列表中:

lst = list(IOLoop.current().run_sync(f))
if lst:
    for row in lst:
        print(row)

相关问题 更多 >