Get psycopg2 count(*) number of results
Question:
Whats the correct way to get the number or rows returned by this query? I’m specifically looking to see if no results are returned.
sql = 'SELECT count(*) from table WHERE guid = %s;'
data=[guid]
cur.execute(sql,data)
results = cur.fetchone()
for r in results:
print type(r) # Returns as string {'count': 0L} Or {'count': 1L}
Thanks.
Answers:
results
is itself a row object, in your case (judging by the claimed print
output), a dictionary (you probably configured a dict-like cursor subclass); simply access the count
key:
result = cur.fetchone()
print result['count']
Because you used .fetchone()
only one row is returned, not a list of rows.
If you are not using a dict(-like) row cursor, rows are tuples and the count value is the first value:
result = cur.fetchone()
print result[0]
The following worked for me
cur.execute('select * from table where guid = %s;',[guid])
rows = cur.fetchall()
print 'ResultCount = %d' % len(rows)
Drawback:
This will not be very efficient for the DBMS if all you need is the count.
This may be helpful to those coming across this thread, here is a way to count all the rows for each table in your database using Python:
total_count = 0
with con.cursor() as cur:
cur.execute("""SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public'""")
for table in cur.fetchall():
table_name = table[0]
cur.execute(sql.SQL("SELECT COUNT(*) FROM {table}").format(table=sql.Identifier(table_name)))
table_count = cur.fetchone()
result = f'TABLE NAME: {table_name}, COUNT: {table_count}'
total_count += int(table_count[0])
print(result)
print(total_count)
Whats the correct way to get the number or rows returned by this query? I’m specifically looking to see if no results are returned.
sql = 'SELECT count(*) from table WHERE guid = %s;'
data=[guid]
cur.execute(sql,data)
results = cur.fetchone()
for r in results:
print type(r) # Returns as string {'count': 0L} Or {'count': 1L}
Thanks.
results
is itself a row object, in your case (judging by the claimed print
output), a dictionary (you probably configured a dict-like cursor subclass); simply access the count
key:
result = cur.fetchone()
print result['count']
Because you used .fetchone()
only one row is returned, not a list of rows.
If you are not using a dict(-like) row cursor, rows are tuples and the count value is the first value:
result = cur.fetchone()
print result[0]
The following worked for me
cur.execute('select * from table where guid = %s;',[guid])
rows = cur.fetchall()
print 'ResultCount = %d' % len(rows)
Drawback:
This will not be very efficient for the DBMS if all you need is the count.
This may be helpful to those coming across this thread, here is a way to count all the rows for each table in your database using Python:
total_count = 0
with con.cursor() as cur:
cur.execute("""SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public'""")
for table in cur.fetchall():
table_name = table[0]
cur.execute(sql.SQL("SELECT COUNT(*) FROM {table}").format(table=sql.Identifier(table_name)))
table_count = cur.fetchone()
result = f'TABLE NAME: {table_name}, COUNT: {table_count}'
total_count += int(table_count[0])
print(result)
print(total_count)