How to get a single result from a SQL query in python? How to get a single result from a SQL query in python? sqlite sqlite

How to get a single result from a SQL query in python?


I think you're looking for Cursor.fetchone() :

cursor.fetchone()[0]


Or you could write a wrapper function that, given SQL, returns a scalar result:

def get_scalar_result(conn, sql):    cursor=conn.cursor()    cursor.execute(sql)    return cursor.fetchone()[0]

I apologize for the possibly less than syntactically correct Python above, but I hope you get the idea.


If you're not using pysqlite which has the built in cursor.fetchone

cursor.execute("select value from table order by value desc limit 1")