Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count number of records in an SQL database with python

I can't seem to print the number of records in my database:
When I program:

cursor = cnxn.cursor()   
count = cursor.execute("select count(*) from fixtures")  
cursor.commit  
print (count)

(fixtures is the name of my database)
I get:

pyodbc.Cursor object at 0x00000000032FC150  

...rather than the number of records.

I am using pyodbc module on python

like image 778
BigGibb Avatar asked Feb 19 '14 14:02

BigGibb


People also ask

How do you get a COUNT of records in SQL query in Python?

Practical Data Science using Python It may be required to count the number of columns present in a SQL table. This is done using count(*) function with information_schema. columns and the WHERE clause. The WHERE clause is used to specify the name of the table whose columns are to be counted.

How do I COUNT the number of records in SQL?

The COUNT() function returns the number of rows that matches a specified criterion.

What is Rowcount () in Python?

rowcount. This read-only property returns the number of rows returned for SELECT statements, or the number of rows affected by DML statements such as INSERT or UPDATE .

How do I COUNT the number of rows in a mysql table in Python?

The COUNT() function is used to return the number of rows which satisfy a certain condition. The SUM() function is used to return the sum of numerical values in a column in the table. The NULL values are ignored.


1 Answers

For pyodbc, cursor.execute() returns the cursor object itself. You still need to retrieve the results separately.

You could loop over the cursor to get rows; list() can do the looping for you and pull in all rows into a list object:

cursor.execute("select count(*) from fixtures")  
print(list(cursor))

or you can call cursor.fetchall().

For a result set with just one row, you could use:

cursor.execute("select count(*) from fixtures")
result = cursor.fetchone()

cursor.fetchone() returns either one row, or None if there are no results at all.

In all cases rows are sequences of columns, for a one-column result that'll be a tuple with just one value in it.

In your example query, you are fetching a single row, with a single column, so you can get that single value with cursor.fetchone() then using indexing or tuple assignment, e.g.

cursor.execute("select count(*) from fixtures")
fixture_count = cursor.fetchone()[0]

or

cursor.execute("select count(*) from fixtures")
fixture_count, = cursor.fetchone()

You don't need to commit after a SELECT, but you didn't actually call the commit() method either, you are missing the () part. If you are altering data, do remember to use cursor.commit(). Note that cursor.commit() does exactly the same thing as cnxn.commit(); transactions are managed per connection, not per cursor.

However, when not using autocommit, it is easier and better to use the connection as a context manager to ensure a transaction is aborted or committed based on there being any exceptions:

with cnxn:
    # anything in this block is handled with a transaction.

# after the block the transaction is committed, unless there was an exception.
like image 71
Martijn Pieters Avatar answered Oct 20 '22 14:10

Martijn Pieters