Skip to main content

MysqldbPythonWrapper

mysqldb_wrapper is a python module that will help you easily communicate with your database and provides encryption with Fernet using the cryptography module. This module is inspired by SQLAlchemy but is even simpler to use and has less limitations (but is also less complete).

IMPORTANT: This module is only made to use with simple mysql queries. You can still use it with more complex queries, but no interface is provided for those.

Installation

This module is available via pip:

pip install mysqldb_wrapper

Usage

The table

First, you need to create a table. Here's an example:

from mysqldb_wrapper import Base, Id

class Test(Base):
    __tablename__ = "test"

    id = Id()
    another_table_id = Id()
    hashed = bytes()
    number = int(1)
    string = str("string")
    boolean = bool(True)

To explain in details:

  • id = Id() is, as its name implies, the database object id. Always put one Id.
  • another_table_id = Id() is the id of another table in the database.
  • hashed = bytes() will be a hashed string, always declare it empty.
  • number = int(1) will be a int with a default value 1.
  • string = str("string") will be a string with a default value string.
  • boolean = bool(True) will be a boolean with a default value boolean.

Easy, right ?

Note: This implementation does not support complex types, so keep your table simple ;)

The database

You need to create your database by hand Although this module will not create the database for you, it will create the tables automatically !

Note: keep in mind that if your table change (remove fields, change field types, ...), you will need to remove/update them by hand.

To initialize a session with the database, use:

from mysqldb_wrapper import Session

session = Session(DB_USERNAME, DB_PASSWORD, DB_TEST, FERNET_KEY)

So here we have:

  • DB_USERNAME is the username of the mysql user
  • DB_PASSWORD is the password of the mysql user
  • DB_TEST is the name of the database
  • FERNET_KEY is the encryption key. You can create one with Fernet.generate_key(), but keep in mind to always use the same one. (So generate one by hand then keep it somewhere safe)

And now we have all we need to use the mysql database.

Operations

Adding an object

Remember your table ? Just create a new object from it and use the add method of the session with it. Also, the table objects supports **kwargs.

obj = Test(hashed="abcd", string="word")
session.add(obj)

And obj now automatically contains the id generated by the database. Also, this method returns the object in case you want to chain methods.

Updating an object

Using the obj from before:

obj.number = 2553166
obj.string = "a sentence"
obj.boolean = True
session.update(obj)

This method also returns the object.

Deleting an object

And again:

session.delete(obj)

obj now is not in the database anymore but you can still continue to use it, just remember it doesn't have an id (set to 0). Like the others, this method returns the object.

Querying objects

We have more to talk about in this section.

obj = session.query(Test).first()

This one is easy. You put the table you want in query() and you want to get the first result.

obj_list = session.query(Test).all()

Same than before, except this time you retrieve all the results. It is guaranteed to return a list, even if empty.

Now the tricky part:

obj = session.query(Test).where(Test.id == 2).first()

This time, we are still querying in the Test table, but we want a specific result, the one where the id is equal to 2. You don't have to worry about doing anything more, everything is handled by the Base class your Test table inherited from.

Remember we had a hash in our table ? Don't worry, it is handled as well. How does it work ? Like the others field. =)

obj = session.query(Test).where(Test.hashed == "abcd").first()

You can also chain the where methods, use variables and query all objects:

a_string = "something"
list_obj = session.query(Test).where(Test.another_table_id == 2).where(Test.hashed == a_string).all()

IMPORTANT: The where method only works with equality as of now. Also, you can only query by Id and hashes.

Metadata

Release files for mysqldb-wrapper 0.10.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mysqldb-wrapper 0.10.6
File Size Uploaded
mysqldb_wrapper-0.10.6.tar.gz 9.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mysqldb-wrapper 0.10.6
File Interpreter ABI Platform
mysqldb_wrapper-0.10.6-py3-none-any.whl Python 3 none any Details

Total release size: 18.0 kB

Release files / mysqldb_wrapper-0.10.6.tar.gz

Download URL mysqldb_wrapper-0.10.6.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4dac7ccf46bdda7507b7e1a738e475b45ad8bd3cc1424ea19c57d7df4b88d292
BLAKE2b-256 checksum
How to use checksums
39a3abf5535d64ce0918fa58e4324f464eabb381d9c219033c03d067f4877e6d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.26.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.6

Release files / mysqldb_wrapper-0.10.6-py3-none-any.whl

Download URL mysqldb_wrapper-0.10.6-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aa94c9154dd1d413c032a26d2e61f7981d20215ecd944b50547937d4132cf7da
BLAKE2b-256 checksum
How to use checksums
b869cdfdc96737c5ad25e97abb9f996a99d13dc1f492a1b06cd454b485b57850
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.26.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.6

Release history Release notifications | RSS feed

This release

0.10.6 This release

2 release files

0.10.5

2 release files

0.10.4

2 release files

0.10.3

2 release files

0.10.2

2 release files

0.10.1

2 release files

0.10.0

2 release files

0.9.0

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page