How to fetch data from sqlite using python? How to fetch data from sqlite using python? sqlite sqlite

How to fetch data from sqlite using python?


The Connection.execute shortcut returns a cursor instance, which you need to use with fetchall. In your code, you're creating a new, independent cursor.

Thus:

import sqlite3conn = sqlite3.connect('chinook.db')cursor = conn.execute("SELECT * FROM tracks")rows = cursor.fetchall()for row in rows:    print row

Or don't use Connection.execute shortcut, to avoid confusion:

import sqlite3conn = sqlite3.connect('chinook.db')cursor = conn.cursor()cursor.execute("SELECT * FROM tracks")rows = cursor.fetchall()for row in rows:    print row