简体   繁体   中英

Python script hangs on cursor.execute()

I'm a newbie in 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,

1.) (Resolved) My table li_records have 91 rows in it then why I'm getting the rowcount as -1?

2.) Why my script hangs up on 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

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 .

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 [..]

As to why your execute method hangs, I don't know. 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. That way you can check more easily to see if there's anything wrong with the query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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