Skip to main content

MrlDB by Rémi "Mr e-RL" LANGDORPH

Copyright (c) 2019 Rémi LANGDORPH - mrerl@warlegend.net (under MIT license)

mrldb is a powerfull database handler for python

Links:

pypi project

github repo

Install with pypi:

pip install mrldb

This package supports the followings database systems: mariadb, mysql, cassandra, sqlite3

DOCUMENTATION

  • MrlDBCluster(): a cluster of databases, the last created MrlDBCluster can be accessible via mdbcl
    • MrlDBCluster.get(name): return the MrlDB from the alias or the name

    • MrlDBCluster.add(name, db, aliases=[]): add a MrlDB object with the name (overwrite if an object has the same name), and link the aliases

    • MrlDBCluster.addalias(name, aliases): link aliases to the name

    • MrlDBCluster.get_cluster_infos(): return a dict with all the config of the connections {"name": {config...}}

    • MrlDBCluster [name]: return the db from the alias or the name

    • for name, connection in MrlDBCluster: iterate over a list of tuple (dbname, connection)

    • MrlDBCluster.dbs: a dict with all the connections {"conn1": <MrlDB>, ...}

    • MrlDBCluster.aliases: a dict with all the aliases and the realname {"alias": "realname", ...}

from mrldb import MrlDBCluster, mdbcl
mycluster=MrlDBCluster()
  • mdbcl: return the last created MrlDBCluster
print(mdbcl)

MrlDB classes

  • MrlDB base class: (Base of MrlDBCassandra, MrlDBMsql, MrlDBSqlite)

    • MrlDB.insert(table, data): insert a new record
      • table is a str of the table where the insert must be executed
      • data is dict with the columns and the values to insert {"col1": "value1", "col2", 3.14}
    • MrlDB.update(table, data, conds=None): update an existing(s) record(s)
      • table is a str of the table where the update must be executed
      • data is dict with the columns and the values to update {"col1": "value1", "col2", 3.14}
      • conds are a str object as "col1='test' and col2=5" or a NoneType object if you want to update all the records of your table
    • MrlDB.select(table, columns, conds=None): get record(s) value(s)
      • table is a str of the table where the select is executed
      • data is list with the columns to get or a "*" to get all the columns
      • conds are a str object as "col1='test' and col2=5" or a NoneType object if you want to select all the records of your table
    • MrlDB.init(): create (or ignoreif they already exists) the tables from the db structure
    • MrlDB.cursor: a connection, you can use MrlDB.cursor.execute(command)
    • MrlDB.structure: a dict of the db structure or a NoneType object if not specified
    • MrlDB._config: a dict of the connexion config
    • MrlDB._getinfos(): return MrlDB._config
  • MrlDBCassandra(cluster, db=None, structure=None, username=None, password=None): a cassandra cluster handler, require library cassandra-driver

    • you can use the database you want or don't use it
    • username and passsword are only used with PlainTextAuthProvider, if you've configured users and password for your db, else, we're connecting as anonymous
  • MrlDBMsql(host, database=None, structure=None, user=None, password=None): a cassandra cluster handler, require library mysql

    • host is the ip adress of the host or a dns-resolvable name of the host
    • you can use the database you want
    • username and passsword are only used with PlainTextAuthProvider, if you've configured users and password for your db, else, we're connecting as anonymous
  • MrlDBSqlite(file, structure=None, autocommit=0): a sqlite file handler, require base library sqlite3 (not recommanded)

    • the file is sqlite3 db file
    • autocommit is the time in seconds (can be a float) between each autocommit, disabled if set 0 (by default)

STRUCTURE argument

with structure, you can get the column names with the results in a dict for each records

structure is an argument for all the DB classes, it must be a None oject or a dictionnary:

MrlDBCassandra(... ,structure={"table0": {"col1": "integer unique", "col2": "text"}, "table2": {"name": "text"}}, ...)

Tutorial script:

from mrldb import MrlDBCassandra, MrlDBCluster, mdbcl
  • we add a simple cassandra cluster as connection to the mrldb cluster, we set cc0 as an alias to the db
mdbcl.add("cassandracluster0", MrlDBCassandra("127.0.0.2"), aliases=["cc0"])
  • we can add other aliases to this connection:
mdbcl.addalias("cassandracluster0", ["cc0_", "cassandra0", "testcluster"])
  • you can connect to your host with password and username
mdbcl.add("cassandracluster1", MrlDBCassandra("127.0.0.3", username="admin", password="something"))
  • you can specify the database too
mdbcl.add("cassandracluster2", MrlDBCassandra("127.0.0.4", database="mydbtest", aliases=["cc3"]))
  • and the best, you can provide database structure to have advanced features
mdbcl.add("cassandracluster3", MrlDBCassandra("127.0.0.5", database="mydbtest",
structure={"table0": {"col1": "integer unique", "col2": "text"}, "table2": {"name": "text"}}), aliases=["cc3"])

the following examples are working for all the differents database systems

SELECT

  • with the correct structure, the following command will give you for each records a dict with the col name and the value, (you can replace the columns by a "*"
mdbcl.get("cc3").select(table="table0", columns=["col1"], conds=None)

result= [{"col1": 0}, {"col1": 1}...]

  • you can use this without structure, it just return the results without dictionnarys
mdbcl.get("cc2").select(table="table0", columns=["col1"], conds=None)

result= [(0, ), (1, )...]

  • you can use conditions
mdbcl.get("cc3").select(table="table0", columns="*", conds="col2='test'")

result= [{"col1": 0, "col2": "test"}, ...]

INSERT

  • data is a dict with all the values to insert
mdbcl.get("cc3").insert(table="table0", data={"col1": 5, "col2": "ok"})

UPDATE

  • use data as the insert command, you can specify conditions with conds
mdbcl.get("cc3").update(table="table0", data={"col1": 5, "col2": "ok"}, conds="col2='test'")

DB init

  • will create the table (or ignore if exists) as the structure
mdbcl.get("cc3").init()

with structure={"table0": {"col1": "integer unique", "col2": "text"}, "table2": {"name": "text"}}

will execute the following commands:

['CREATE TABLE IF NOT EXISTS table0(col1 integer unique, col2 text)',
 'CREATE TABLE IF NOT EXISTS table2(name text)']

Download files

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

Source Distribution

mrldb-0.0.14.tar.gz (6.8 kB view details)

Uploaded Source

File details

Details for the file mrldb-0.0.14.tar.gz.

File metadata

  • Download URL: mrldb-0.0.14.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for mrldb-0.0.14.tar.gz
Algorithm Hash digest
SHA256 41b16573cbccdb74aaaaee8a636086197d157897675e6c3e897825341c6b15ee
MD5 141b529945fc2bf540317206556e89a0
BLAKE2b-256 1e66ead6e92fb5ff8050743a5775fe5699a600f9c98855f8fa3641f5154bf526

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.5

1 file

0.1.4

1 file

0.1.3

1 file

0.1.2

1 file

0.1.0

1 file

0.0.29

1 file

0.0.28

1 file

0.0.27

1 file

0.0.26

1 file

0.0.25

1 file

0.0.24

1 file

0.0.23

1 file

0.0.22

1 file

0.0.21

1 file

0.0.20

1 file

0.0.19

1 file

0.0.18

1 file

0.0.17

1 file

0.0.16

1 file

0.0.15

1 file

This release

0.0.14 This release

1 file

0.0.13

1 file

0.0.12

1 file

0.0.11

1 file

0.0.10

1 file

0.0.9

1 file

0.0.8

1 file

0.0.7

1 file

0.0.6

1 file

0.0.5

1 file

0.0.4

1 file

0.0.3

1 file

0.0.2

1 file

0.0.1

1 file

Supported by

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