How to print output from sqlite3 in python

Viewed 6260

Here is my code:

 conn=sqlite3.connect('myfile.db')
 print(conn.execute("PRAGMA table_info(mytable);"))

When I run that I got this output:

sqlite3.Cursor object at 0x02889FAO

How can I print the actual sqlite3 output of that?

1 Answers

You should to fetch results. Here is working example:

import sqlite3

conn = sqlite3.connect('myfile.db')
cursor = conn.execute("PRAGMA table_info(mytable);")
results = cursor.fetchall()
print(results)

Or with pretty print:

import sqlite3
from pprint import pprint

conn = sqlite3.connect('myfile.db')
cursor = conn.execute("PRAGMA table_info(mytable);")
results = cursor.fetchall()
pprint(results)
Related