如何在 python 中打印 sqlite3 的输出

Nuh*_*min 4 python printing sqlite

这是我的代码:

 conn=sqlite3.connect('myfile.db')
 print(conn.execute("PRAGMA table_info(mytable);"))
Run Code Online (Sandbox Code Playgroud)

当我运行时,我得到以下输出:

sqlite3.Cursor 对象位于 0x02889FAO

我怎样才能打印它的实际 sqlite3 输出?

小智 5

您应该获取结果。这是工作示例:

import sqlite3

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

或者用漂亮的印刷:

import sqlite3
from pprint import pprint

conn = sqlite3.connect('myfile.db')
cursor = conn.execute("PRAGMA table_info(mytable);")
results = cursor.fetchall()
pprint(results)
Run Code Online (Sandbox Code Playgroud)