Skip to content Skip to sidebar Skip to footer

Calculate A Mean With Python Sqlite3 Using The Cursor

I have a database containing a table and I want to compute the mean of a column without importing all of the rows and doing it directly in python; I think doing that would take lon

Solution 1:

Not sure of your other code, but I think you're looking for the .fetchone() method. Something along these lines should work:

import sqlite3
conn = sqlite3.connect('example.db')
c = conn.cursor()
c.execute('SELECT AVG(column) FROM table')
print(c.fetchone())

.fetchone() method returns one result, if you're looking to return more than one result from an execute you would use a loop like:

for row in c.execute('SELECT * FROM table')
    print(row)

Post a Comment for "Calculate A Mean With Python Sqlite3 Using The Cursor"