How to query an SQLite database using Constellab?
In this article, we show how to use a simple Python live code to query an SQLite database using Pandas into Constellab.
Input data
The input data is a SQLite db file with a table named demo
Live code
The following Python live code select the table demo
and converts it to a Pandas DataFrame. This DataFrame is sent to the output the task and can be displayed in a table.
import sqlite3
import pandas as pd
from gws_core import Table
def load_table_into_dataframe(db_file, table_name):
# Connect to the SQLite database
conn = sqlite3.connect(db_file)
# Create a pandas DataFrame by fetching the entire table from the database
query = f"SELECT * FROM {table_name};"
df = pd.read_sql_query(query, conn)
# Close the database connection
conn.close()
return df
# The name of the table to select
table_name = 'demo'
# Load the table into a pandas DataFrame
df = load_table_into_dataframe(sources[0].path, table_name)
targets = [Table(df)]
Parameters
No parameters are used.
Results
This is the content of the selected table demo
rendered as a table
Table of contents
Input data Live code Parameters Results Table of contents