Skip to main content

Python3 tool to create and handle very quickly a sqlite3 database in an application

Project description

logo silly db

Silly DB

It is ...

Not an ORM, even though it may "look and feel like" one in some aspects.

  • Build the structure of your DB from classic .sql files.
  • Queries return directly usefull python objects like an ORM would do.

Some SQL knowledge is required, the purpose of Silly DB is not to get rid of SQL (actualy, SQL is the best tool to manage... a SQL database), but to handle the boring things, and let you focus on your application.

The required knowledge and much more is available here :

You should consider using 'DB Browser for SQLite':

No need to be an expert, just understand own to create a DB and use 'SELECT' will be fine (see the examples files to get quickly some bases).

Installation

$ pip install silly_db

Fast way to begin

Create a new directory and open a console in there.

Get a basic working structure with 'plop':

$ python3 -m silly_db.plop db

Congratulations ! You've got your database ready to work ! To understand how it works, open the differents files provided and read the comments, it will be easy to adapt to your own needs.

get more info with:

$ python3 -m silly_db

and more about the plop options here:

$ python3 _n silly_db.plop

Silly DB gives a hand with:

  • DB object (methods: execute, migrate, export, export_structure, select)

  • Selection / SelectionItem

  • Selection.exists() -> bool

  • Selection.jsonify() -> list of dict

  • Selection.__add__() (new = selection1 + selection2 #without duplications)

  • Selection.order_by(key='a_column_name', reverse=False)

  • SelectionItem are convertible to dict: dico = dict(SelectionItem)

  • Migrations made easy

  • Quick start with plop

And some more... fix the sql 'quotes' problem with silly_db.helpers.to_sql:

name = to_sql("tim's") # convert ' into '' (escaped quote)
db.execute(f"INSERT INTO 'guys' ('name') VALUES('{name}')")

About queries

Queries are done just as it is in SQL, but with the DB.select() method. A query returns a Selection object (list of SelectionItem objects). Attributes are automaticaly gathered from the query.

A Selection object can be converted into a json format.

>>> some_json = selection.jsonify()

A SelectionItem object can be converted into a dict:

>>> dico = dict(selection_item)

Short example:

>>> selection = db.select(
    "cat.name as cat, person.name as owner FROM cat JOIN person ON "
    "cat.owner_id=person.id WHERE cat.owner_id=1"
    )
>>> print(selection)
<Selection[{cat: Chat, owner: Irina, }, {cat: snow_ball, owner: Irina, }, ]>
>>> selection.items[1].cat
'snow_ball'

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

silly-db-1.0.7.tar.gz (8.2 kB view hashes)

Uploaded Source

Built Distribution

silly_db-1.0.7-py3-none-any.whl (10.9 kB view hashes)

Uploaded Python 3

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