Create an SQLite table if missing or check that the existing table matches the given creation statement.
Project description
title: README author: Jan-Michael Rye
Synopsis
A Python package that provides a simple utility function for creating SQLite tables. The function will compare an existing table's SQL statement from the sqlite_master
table to the one expected for a given SQL table creation statement. If the existing table does not match the expected SQL statement then it can either be dropped an recreated, or a ValueError
can be raised.
The primary purpose of this function is to facilitate the creation of tables for caching temporary data that can be discarded, such as memoizing calls to remote server APIs.
Links
GitLab
Other Repositories
Usage
import sqlite3
from sqlite3_create_and_check_table import create_and_check_table
sql = "CREATE TABLE test (prim TEXT PRIMARY KEY, int INTEGER, blob BLOB NON NULL)"
conn = sqlite3.connect("/path/to/some/sqlite3/db.sqlite")
with conn as cur:
# Create the table with the given SQL statement. If a table with the same
# name exists but differs from what would result from the given SQL table
# creation statement, it will be dropped and recreated to match the given
# statement.
# If drop is set to False then a ValueError will be raised instead.
create_and_check_table(cur, sql, drop=True)
The package also provides a row factory function for returning rows in the table as dict
s as well as an sqlite3 cursor context manager for temporarily setting the the row factory to the provided dict
factory:
import sqlite3
from sqlite3_create_and_check_table import dict_factory, dict_factory_cursor
conn = sqlite3.connect("/path/to/some/sqlite3/db.sqlite")
# Set the row factory within a context and restore the previous row factory when
# leaving the context.
with dict_factory_cursor(conn) as cur:
# cur.execute(...)
# Set the row factory for all queries.
conn.row_factory = dict_factory
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sqlite3_create_and_check_table-2024.2.tar.gz
.
File metadata
- Download URL: sqlite3_create_and_check_table-2024.2.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aff9220521f2eb5660695e948f65652347f9458e3dd434f4d6784cb4a9969bf2 |
|
MD5 | 629d9fe10f14574566dfe5aec1a4c526 |
|
BLAKE2b-256 | 53b68680762b237c7657686ea35320b1fac096ecc8a4f15c58ed3e6657fa9f16 |
File details
Details for the file sqlite3_create_and_check_table-2024.2-py3-none-any.whl
.
File metadata
- Download URL: sqlite3_create_and_check_table-2024.2-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fd499c6b9e3abe4afb77ba977acb07c6144ff71b4ab95dbc26a4f1755e4dfeb1 |
|
MD5 | 7b8ff04532ec854d08cfec49189fd5f4 |
|
BLAKE2b-256 | 92ebdc11f49b15a879ae24187a08063e9f662c1fc695de381388de8f246594cb |