I am using this sample sqlite database and my code is
import sqlite3
conn=sqlite3.connect('chinook.db')
conn.execute("SELECT * FROM tracks")
rows= conn.cursor().fetchall()
for row in rows:
print row
It should have worked but there is no output? What am I doing wrong here?
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 sqlite3
conn = 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 sqlite3
conn = sqlite3.connect('chinook.db')
cursor = conn.cursor()
cursor.execute("SELECT * FROM tracks")
rows = cursor.fetchall()
for row in rows:
print row
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.