Python script hangs on cursor.execute()
<p><strong>I'm a newbie in Python</strong></p>

<p>I'm debugging an existing script which is as follows,</p>

<pre><code>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"
</code></pre>

<p>I'm getting the below output,</p>

<pre><code>Table name: li_records
Between: 4 and 6
Total Rows: -1
</code></pre>

<p>I have 2 questions here,</p>

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

<p>2.) Why my script hangs up on <code>cursor.execute</code>?</p>

<p><strong>Question 1 resolved: Like many of you pointed out the reason I'm getting '-1' is because I have not executed the query yet</strong></p>

<p>Any help is appreciated.
Thanks in advance.</p>

<p>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 <a href="https://docs.python.org/2/library/sqlite3.html#sqlite3.Cursor.rowcount" rel="nofollow">the docs on rowcount</a>.</p>

<p>It states:</p>

<blockquote>
  <p>As required by the Python DB API Spec, the rowcount attribute âis -1
  in case no executeXX() has been performed on the cursor [..]</p>
</blockquote>

<p>As to why your <code>execute</code> method hangs, I don't know. Could you construct the query string outside of the method call like so:</p>

<pre><code>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)
</code></pre>

<p>If you do it like that, you can also <code>print</code> it before executing. That way you can check more easily to see if there's anything wrong with the query.</p>

python mysql