Skip to main content

This is a python package that makes it easier to work with sqlite

Project description

Sqlite middleware

This package removes all the annoying boilerplate code you have to write when using sqlite3. Instead, use the methods this package provides, and easily manage your sqlite database in your python project.

Installation

For more information, check out the PyPi page

To install the newest version, use:

pip install sqlite-middleware

Db class:

All the functionality of this package is in the Db class. When creating a new instance of this class, you should specify following parameters:

  • database: (str) name of the sqlite file

Example usage:

db = Db("demo.db")

When an instance of Db is deleted, the __del__ magic method will close the connection.

Methods

Here are all the methods that the Db class provides:

Select all

This method will retrieve and return all objects of a certain class from the sqlite database.

Parameters:

  • cls: (Class) class of the objects you want to retrieve

Example usage:

db.select_all(Person)

Select by id

This method will retrieve and return an object with certain id and class from the database.

Parameters:

  • cls: (Class) class of the object you want to retrieve
  • id: (int) id of the object you want to retrieve

Example usage:

db.select_by_id(Person, 2)

Select by custom attribute

This method will retrieve and return an object with certain value for a certain attribute

Parameters:

  • cls: (Class) class of the object you want to retrieve
  • custom_attribute: (str) string of the column of the attribute you want to search for
  • value: (any) value of this attribute you are searching for

Example usage:

db.select_by_custom_attribute(Person, "name", "John Doe")

Create table

This method will create a table in the database for objects of a certain class.

Parameters:

  • object: (Instance) instance of the class you want to create a new table for.

Example usage:

db.create_table(Person())

Save object

This method will save an object in the database.

It will also create a table if it doesn't exist yet.

Parameters:

  • object: (Instance) object you want to save in the database.

Example usage:

person = Person("John Doe")
db.save_object(person)

Update object

This method will update an existing object in the database.

Parameters:

  • id: (int) id of the object you want to update.
  • object: (Instance) object containing new values you want to save in the database.

Example usage:

person = Person("John Doe")
db.save_object(person) # Say this is the first person, and gets id 1

person.set_name("Michael Myers")
db.update_object(1, person)

Delete object

This method will delete an object from the database.

Parameters:

  • cls: (Class) class of the object you want to delete.
  • id: (int) id of the object you want to delete

Example usage:

person = Person("John Doe")
db.save_object(person) # Say this is the first person, and gets id 1

db.delete_object(Person, 1)

How does it work

Table names

By default table names are created with the name of the class you specified. tbl[Classname]s

The reason that we must pass an object, and not a class, to db.create_table(), is because it takes all the instance variables of this object and uses them to create a column in the sqlite table.

For example with a class named Person, which has a name parameter:

db.create_table(Person())

The line above would create a table, with name tblPersons and it would have 2 columns name, and also an auto-generated column for id.

As we need id's to specify what object to delte, the id needs to be stored in the database, this is done automatically when creating a table / saving an object.

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

sqlite_middleware-0.0.3.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlite_middleware-0.0.3-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file sqlite_middleware-0.0.3.tar.gz.

File metadata

  • Download URL: sqlite_middleware-0.0.3.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for sqlite_middleware-0.0.3.tar.gz
Algorithm Hash digest
SHA256 a3b841404316402f2a1f10b10818c6c3c90cf42e1849134a381d4775d85aa2ff
MD5 49e3207eac118e2760e5c64720ece0e8
BLAKE2b-256 f6e013c0e3b446310c7a1ac5c35721f0cca2ff240d2930097e0020decfaa0d3b

See more details on using hashes here.

File details

Details for the file sqlite_middleware-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlite_middleware-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 646968896fd49f211a8d9cf0956d1162ed73396c2151cb4ba12db9cda8f95351
MD5 5f52e591824c14fa47b2a9ce44e619b6
BLAKE2b-256 8e239955fee22768b41d98d6b005946c0ce0605559ee5207e129f5583fd92208

See more details on using hashes here.

Supported by

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