1

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.

13
  • 1
    You need to call cursor.rowcount after cursor.execute() Commented Sep 23, 2016 at 20:49
  • 1
    I believe db is a connection object, correct? Commented Sep 23, 2016 at 20:51
  • 1
    Ok, did you tested your query in the server? does it work there and give you the intended results? Commented Sep 23, 2016 at 20:53
  • 1
    If 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. Commented Sep 23, 2016 at 20:58
  • 1
    Could you try without the between clause? Maybe, that could be giving the error, try the integer inequalities for instance. Commented Sep 23, 2016 at 21:05

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you Jasper, I understood the problem with my first question. Like you suggested I'm printing out the query before executing it and it is just like I expected.
Mmm, and copying the printed query and executing it through sqlite directly gives you the expected results?
I'm not using sqlite but an alternative called 'Emma', none of my query runs on that application, not even a simple 'select * from tablename' because it crashes up. Do you think this could be the reason?
I don't know Emma, so I can't comment on any specifics about that. Are you using the built-in sqlite module in Python, in the hopes that Emma has a compatible enough API, or are you using a special Emma module? I found this on Github: github.com/myemma/EmmaPython
Haha, sorry. I just discover that what I linked is totally not what you're talking about. I can't find anything named 'Emma' that is even close to a database. Could you provide me with a link?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.