简体   繁体   English

Python 脚本挂在 cursor.execute() 上

[英]Python script hangs on cursor.execute()

I'm a newbie in Python我是 Python 新手

I'm debugging an existing script which is as follows,我正在调试一个现有的脚本,如下所示,

print "Table name: %s " %table_name_r
print "Between: %s " % between

cursor = db.cursor()
print "Total Rows: %s " % cursor.rowcount

cursor.execute("""select contactid,li_url_clean,li_company,complink from """+ table_name_r +""" where li_company is not null and id_auto between """+between)

print "Execution complete"

I'm getting the below output,我得到以下输出,

Table name: li_records
Between: 4 and 6
Total Rows: -1

I have 2 questions here,我在这里有2个问题,

1.) (Resolved) My table li_records have 91 rows in it then why I'm getting the rowcount as -1? 1.)(已解决)我的表li_records有 91 行,那么为什么我将行数设为 -1?

2.) Why my script hangs up on cursor.execute ? 2.) 为什么我的脚本挂在cursor.execute

Question 1 resolved: Like many of you pointed out the reason I'm getting '-1' is because I have not executed the query yet问题 1 已解决:就像你们中的许多人指出我得到“-1”的原因是因为我还没有执行查询

Any help is appreciated.任何帮助表示赞赏。 Thanks in advance.提前致谢。

You haven't executed the query yet, so the database doesn't know the amount of rows that will be in the result.您尚未执行查询,因此数据库不知道结果中的行数。 See also the docs on rowcount .另请参阅rowcount 上的文档

It states:它指出:

As required by the Python DB API Spec, the rowcount attribute “is -1 in case no executeXX() has been performed on the cursor [..]根据 Python DB API 规范的要求,rowcount 属性“是 -1,以防没有在游标上执行 executeXX() [..]

As to why your execute method hangs, I don't know.至于为什么你的execute方法挂起,我不知道。 Could you construct the query string outside of the method call like so:您能否像这样在方法调用之外构造查询字符串:

query = "select contactid,li_url_clean,li_company,complink from " + table_name_r + " where li_company is not null and id_auto between " + between
cursor.execute(query)

If you do it like that, you can also print it before executing.如果你这样做,你也可以在执行前print它。 That way you can check more easily to see if there's anything wrong with the query.这样您就可以更轻松地检查查询是否有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM