How to unpack list of data from sqlite database into a table? #3221
Answered
by
codenotworking
codenotworking
asked this question in
Q&A
-
I'm trying unpack a list of arbitrary amount of results from a sqlite database and have them rendered in a table. Is this possible? Example simplified code: import sqlite3
from rich.console import Console
from rich.table import Table
# Truncated sqlite code
rows = cursor.fetchall() # fetchall() returns a list
table = Table()
for row in rows:
table.add_row() # How to unpack "rows" contents into add_row()?
console = Console()
console.print(table) |
Beta Was this translation helpful? Give feedback.
Answered by
codenotworking
Dec 3, 2023
Replies: 1 comment
-
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, fetchall() returns a list of tuples. Example:
# rows = [
# ("foo", "bar", 1),
# ("foo", "bar", 3),
# ("foo", "bar", 12),
# ("foo", "bar", 34)
# ]
# With sqlite3.Row as the row factory, fetchall() returns a list of Row objects. Example:
# rows = [
# <sqlite3.Row object at 0x0000015935E43D00>,
# <sqlite3.Row object at 0x0000015935E43D60>,
# <sqlite3.Row object at 0x0000015935E43D90>,
# <sqlite3.Row object at 0x0000015935E43DC0>
# ]
# Get the column names from the sqlite3.Row object (first item in list is enough to do this)
# Use the list to populate the add_column() further down
column_names = rows[0].keys()
# Convert data to Text object
# Using str() to cast integers to string if database has integer type, for Text to use
# NESTED LIST COMPREHENSION VERSION
rows_text = [[Text(str(row)) for row in row_item] for row_item in rows]
# OR...
# NESTED FOR LOOP VERSION (If you want readability)
# rows_text = []
# for i, row_item in enumerate(rows):
# # Add a sublist
# rows_text.append([])
# for row in row_item:
# rows_text[i].append(Text(str(row)))
# Create table
table = Table(box=box.ROUNDED, title="DEMO")
# Build columns
for name in column_names:
# Use * to unpack list of strings
table.add_column(*name)
# Build rows
for row in rows_text:
# Use * to unpack list of Text objects
table.add_row(*row)
# Display table
console = Console()
console.print(table) # OUTPUT
DEMO
╭─────────┬─────────┬────────╮
│ Column1 │ Column2 │ Number │
├─────────┼─────────┼────────┤
│ foo │ bar │ 1 │
│ foo │ bar │ 3 │
│ foo │ bar │ 12 │
│ foo │ bar │ 34 │
╰─────────┴─────────┴────────╯ |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
codenotworking
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I did some experimenting using Text with Table and found the solution. Sharing some basic code for anyone else that needs it.