Skip to content

How to unpack list of data from sqlite database into a table? #3221

Discussion options

You must be logged in to vote

I did some experimenting using Text with Table and found the solution. Sharing some basic code for anyone else that needs it.

import sqlite3

from rich import box
from rich.console import Console
from rich.table import Table
from rich.text import Text


# Truncated sqlite code
sqlite3.connect("path/to/database.db")
# I use the built-in sqlite3.Row object since the keys() method
# allows you to get the column names in the database (see further down)
# https://docs.python.org/3/library/sqlite3.html#row-objects
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute(" SQL STATEMENT HERE ")
rows = cursor.fetchall()    # fetchall() returns a list of Row objects


# Normally, fetch…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by codenotworking
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant