Working with SQLite in Python
SQLite database file handling with table loading and saving capabilities.
Installation
Section titled “Installation”pip install fairspecGetting Started
Section titled “Getting Started”The SQLite plugin provides:
load_sqlite_table- Load tables from SQLite databasessave_sqlite_table- Save tables to SQLite databasesSqlitePlugin- Plugin for framework integration
For example:
from fairspec import load_sqlite_table, Resourcefrom fairspec_metadata import SqliteFileDialect
table = load_sqlite_table(Resource( data="database.db", fileDialect=SqliteFileDialect(tableName="users"),))# column types will be automatically inferred from database schemaBasic Usage
Section titled “Basic Usage”Loading SQLite Tables
Section titled “Loading SQLite Tables”from fairspec import load_sqlite_table, Resourcefrom fairspec_metadata import SqliteFileDialect
# Load a table from SQLite databasetable = load_sqlite_table(Resource( data="data.db", fileDialect=SqliteFileDialect(tableName="products"),))
# Load from a specific pathtable = load_sqlite_table(Resource( data="/path/to/database.db", fileDialect=SqliteFileDialect(tableName="orders"),))Saving SQLite Tables
Section titled “Saving SQLite Tables”from fairspec import save_sqlite_tablefrom fairspec_metadata import SqliteFileDialect
# Save table to SQLite databasesave_sqlite_table(table, path="output.db", fileDialect=SqliteFileDialect( tableName="results",))
# Overwrite existing tablesave_sqlite_table(table, path="output.db", fileDialect=SqliteFileDialect( tableName="results",), overwrite=True)Advanced Features
Section titled “Advanced Features”Schema Inference
Section titled “Schema Inference”Table schemas are automatically inferred from SQLite table definitions:
from fairspec import load_sqlite_table, Resourcefrom fairspec_metadata import SqliteFileDialect
# Field types are automatically detected from database schematable = load_sqlite_table(Resource( data="shop.db", fileDialect=SqliteFileDialect(tableName="products"),))# Types like INTEGER, TEXT, REAL are mapped to appropriate Table Schema typesCreating New Tables
Section titled “Creating New Tables”When saving, the plugin automatically creates the table structure:
from fairspec import save_sqlite_tablefrom fairspec_metadata import SqliteFileDialect
# Creates a new database file with the specified tablesave_sqlite_table(table, path="new-database.db", fileDialect=SqliteFileDialect( tableName="my_data",))Working with Table Schema
Section titled “Working with Table Schema”You can provide a custom Table Schema when saving:
from fairspec import save_sqlite_tablefrom fairspec_metadata import SqliteFileDialect, TableSchema, IntegerColumnProperty, StringColumnProperty
save_sqlite_table(table, path="output.db", fileDialect=SqliteFileDialect( tableName="customers",), tableSchema=TableSchema(properties={ "id": IntegerColumnProperty(), "name": StringColumnProperty(), "email": StringColumnProperty(),}))