Skip to main content

a simple SQLite query builder with a few bells and whistles

Project description

GitHub issues GitHub license PyPI

HissDB is a SQLite3 frontend with a focus on ease-of-use and tight integration with Python syntax.

Installation

python3 -m pip install hissdb

Usage

Database Setup

from hissdb import Database

# make a new db or load one from an existing path
db = Database('test_location.db')

# add some tables to the database
users = db.create_table(
	id = 'INTEGER PRIMARY KEY',
    first_name = 'TEXT',
    last_name = 'TEXT',
    age = 'INTEGER',
)
posts = db.create_table(
	user_id = 'INTEGER NOT NULL',
    text = 'TEXT',
    date = 'INTEGER',
    foreign_keys = {'user_id': users.id},
)

Writing Data

# inserting a row returns the new rowid
jane_id = users.insert(first_name = 'Jane', last_name = 'Doe')
john_id = users.insert(first_name = 'John', last_name = 'Doe')

posts.insert(
    user_id = john_id,
    date = 20210817,
    text = "I'm John Doe and this is my first post!"
)

# you can also insert many rows at once using a list or generator
posts.insertmany(
    cols = ['user_id', 'date', 'text'],
    rows = [
    	(jane_id, 20210814, "First!"),
    	(jane_id, 20210816, "The weather is nice today."),
    	(jane_id, 20210817, "Do you ever post on the internet just so there's content?"),
	],
)

# you can update data based on matching criteria.
# for instance, let's add a signature to each of Jane's posts
posts.update(
    text = posts.text + ' - ' + users.first_name,
    where = users.id == jane_id,
)

# finally, we must write the changes to the file
db.commit()

Reading Data

# get all users
names = users.fetchall(cols=['first_name', 'last_name'])
assert names == [('Jane', 'Doe'), ('John', 'Doe')]

# get a single column
first_names = users.first_name.fetchall()
assert first_names == ['Jane', 'John']


# easily write WHERE queries
does = users.id.fetchall(where=users.last_name == 'Doe')
# an even simpler equivalent:
does = users.id.fetchall(last_name='Doe')
assert does = [1, 2]

# a few methods like startswith() have been translated to SQL expressions
users.insert(first_name='Dave', last_name='Guy')
non_j_name = users.first_name.fetchone(~users.first_name.startswith('J'))
assert non_j_name == 'Dave'

# you can construct all kinds of queries
full_names = users.fetchall(cols=(users.first_name + ' ' + users.last_name))
assert full_names == [('Jane Doe',), ('John Doe',), ('Dave Guy',)]

For more sample code using HissDB, see the tests.

If you're looking for more detailed documentation, check out the library reference.

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

hissdb-0.0.2.tar.gz (30.7 kB view details)

Uploaded Source

Built Distribution

hissdb-0.0.2-py3-none-any.whl (32.1 kB view details)

Uploaded Python 3

File details

Details for the file hissdb-0.0.2.tar.gz.

File metadata

  • Download URL: hissdb-0.0.2.tar.gz
  • Upload date:
  • Size: 30.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.9.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for hissdb-0.0.2.tar.gz
Algorithm Hash digest
SHA256 d8eaaa8643a211bbba9603913fa3c7a8263e586d02f3abfc7363fd8de1938813
MD5 3fb1b56f3dbb0e1fe6fbb6129637fe1c
BLAKE2b-256 64e1ce97abb57701f8139cd2b86bfa353892e4f48bac0e9a48cbca8424c98f26

See more details on using hashes here.

File details

Details for the file hissdb-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: hissdb-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 32.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.9.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for hissdb-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2f8ba3e291dc6d63710bddcab0a6db5d98c67892abc2d62f40e75ee6356cea15
MD5 82de40f2f9a551e1a968f94474ad53de
BLAKE2b-256 667a2cd9073c274275e4d2de6fe530e022eb232803e1095318e3602ea7d8a504

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