0

I need to check if table which holds credentials has record with matched username and password. I use COUNT because it's easier to parse one int value. How to check (using Python) if there are 1 or more records in credentials table?

    query = "SELECT COUNT(user_name) from credentials user_name='[email protected]' and pwd='123';"
    con = sqlite.connect(databasefile)
    cur = con.cursor()
    cur.execute(query)
    while 1:
        row=cur.fetchone()
        if not row:
            break
        res=res+[row]
#check if there is 1 or more matched records .... 

It's the result from server:

sqlite> SELECT COUNT(user_name) from credentials where  user_name='[email protected]' and pwd='1           23';
COUNT(user_name)
----------------
1

1 Answer 1

1

From PEP 249:

.rowcount

This read-only attribute specifies the number of rows that the last .execute*() produced (for DQL statements like SELECT) or affected (for DML statements like UPDATE or INSERT).

if cur.rowcount > 0:
   ...
Sign up to request clarification or add additional context in comments.

2 Comments

cur.rowcount is equal to -1 but I am sure the SQL is correct. Why?
I think as far as .rowcount is not working for SELECT I should use len(row) ...

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.