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.
-
1You need to call cursor.rowcount after cursor.execute()picmate 涅– picmate 涅2016年09月23日 20:49:12 +00:00Commented Sep 23, 2016 at 20:49
-
1I believe db is a connection object, correct?picmate 涅– picmate 涅2016年09月23日 20:51:30 +00:00Commented Sep 23, 2016 at 20:51
-
1Ok, did you tested your query in the server? does it work there and give you the intended results?picmate 涅– picmate 涅2016年09月23日 20:53:08 +00:00Commented Sep 23, 2016 at 20:53
-
1If the query is unresponsive in the client, then it is not a problem in your python code. It is a possible problem in your sql query.picmate 涅– picmate 涅2016年09月23日 20:58:59 +00:00Commented Sep 23, 2016 at 20:58
-
1Could you try without the between clause? Maybe, that could be giving the error, try the integer inequalities for instance.picmate 涅– picmate 涅2016年09月23日 21:05:04 +00:00Commented Sep 23, 2016 at 21:05
1 Answer 1
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.