Skip to main content

SQLite3 I/O library

Project description

sqliteio is a library for reading and writing SQLite3 file with its own API.

I have tested it with recent CPython and MicroPython. Working with MicroPython is one of the key motivations.

It can be used with CPython. But since it is written in pure python, it is too slow. And has limited functionality. So there is no advantage to using it with CPython.

I started this project to learn about sqlite3 data structures. I hope this is helpful for those learning about sqlite3’s B+ tree structure.

Examples

Open & Close

Everything starts with open() and ends with close()

import sqliteio

database = sqliteio.open('/path/to/db_name.sqlite')

(... operations to database)

database.close()

open() as read only file.

f = open("/path/to/db_name.sqlite", "rb")
database = sqliteio.open(f)

You can also open() with a BytesIO instance or something byte stream generator.

with open("/path/to/db_name.sqlite", "rb") as f:
    bytesio = io.BytesIO(f.read())
    database = sqliteio.open(bytesio)

Fetch all records

Retrieve all data in a table.

for rowid_record in database.fetch_all("table_name"):
    (rowid, r) = rowid_record
    print(rowid)    # print rowid
    print(r)        # print record dict

Get by rowid

Retrieve a record using rowid.

Returns None if there is no record for the rowid.

rowid_record = database.get_by_rowid("table_name", 10)
assert rowid_record[0] == 10
print(r)        # print record dict

Get by Primary Key

Retrieve a record using Primary Key.

Returns None if there is no record for the primary key.

rowid, r = database.get_by_pk("table_name", 10)
assert rowid == 10
print(r)

Filter

Retrieve the target records using a table name and conditions.

cond = {
    "column1': 1,
    "column2": "str",
}
for rowid_record in database.filter("test_table", cond):
    (rowid, r) = rowid_record
    print(rowid)    # print rowid
    print(r)        # print record dict

Insert

Insert using dictionary list data.

r1 = {
    'pk_column': None,
    'column1': 1,
    'column2': 'string1',
}
r2 = {
    'pk_column': None,
    'column1': 2,
    'column2': 'string2',
}

database.insert("table_name", [r1, r2])

Delete

Deletion by rowid. In other words, sqliteio only has the ability to delete one row at a time.

database.delete_by_rowid("test_table", 1)

Update

Update by rowid. In other words, sqliteio only has the ability to update one row at a time.

update_data = {
    'column1': 10,
    'column2': 'new_data',
}
database.update_by_rowid("test_table", 1, update_data)

Commit & Rollback

With Insert, Delete and Update, only the data in memory can be changed and reflected in the file with commit(). To discard changes, use rollback().

database.commit()
database.rollback()

Reference for development

Reference for reading and writing the source code.

Documents on the web

Repository

Book

  • Alex Petrov, A Deep Dive into How Distributed Data Systems Work, O’Reilly Media, Inc. 2019 (chapter 3,4)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqliteio-0.3.0.tar.gz (19.3 kB view details)

Uploaded Source

File details

Details for the file sqliteio-0.3.0.tar.gz.

File metadata

  • Download URL: sqliteio-0.3.0.tar.gz
  • Upload date:
  • Size: 19.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.12

File hashes

Hashes for sqliteio-0.3.0.tar.gz
Algorithm Hash digest
SHA256 9760e10a2b9868557286599d616926375e6588c02e914ae448f28cd153cc7bdc
MD5 ae731e61acbee0ae1d3bc542606e8453
BLAKE2b-256 18da88353c7356a0db1f04dd623a4428eddb5fe856be38fd00a2a322f98b7ed4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page