【问题标题】:Live output from Python database queryPython 数据库查询的实时输出
【发布时间】:2013-03-07 22:53:58
【问题描述】:

我有一段代码如下所示:

 conn=sybpydb.connect(user=args.u, password=args.p,servername=args.s)
 cur = conn.cursor()
 cur.execute(sql)
 print(cur.connections.messages)

执行可能需要 5 分钟才能运行,然后它会打印我的输出。 我想知道是否有任何方法可以在执行运行时打印输出行而不是等到它完成并将其全部作为一个大批量获取?

【问题讨论】:

  • 您是否尝试遍历光标?
  • 我认为您需要像服务器端光标功能这样的东西。例如 MySQLdb 接口支持:stackoverflow.com/questions/337479/…。不确定 sybpydb 是否可以处理您的情况。
  • 你说的迭代光标是什么意思?

标签: python database sybase output sap-ase


【解决方案1】:

filmor 是对的:检索光标上运行的结果。

sybpydb 包是 Sybase 提供的 PEP 249 实现。因此,您检索的 Cursor 对象有一些方法,例如 fetchone 和 fetchall。

您的代码,逐行检索结果并对其进行处理,将是:

# Preparation your connection and cursor for fetching results
conn = sybpydb.connect(user=args.u, password=args.p, servername=args.s)
cur = conn.cursor()
cur.execute(sql)

# Reading result one by one
for row in cur.fetchone():
    # Do something with your current results
    print(row)

# Closing cursor and connection
cur.close()
conn.close()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    • 2015-11-26
    相关资源
    最近更新 更多