Mandb Documentation

Mandb is a lightweight wrapper around MySQLdb and sqlite3.

This lib is inspired by torndb and DBUtils. It supports DBUtils to manage your exists connection. If you has any good ideas, please contact me <kehr.china@gmail.com>

exception mandb.MandbEception[source]

Base exception for mandb

class mandb.Row[source]

A dict that allows for object-like property access syntax.

class mandb.Database(connection=None, **kwargs)[source]

This class provide a series of base database operations. It can manage your connection, if you already has one.

Example:

import MySQLdb
from mandb import Database
from DBUtils.PooledDB import PooledDB

pdb = PooledDB(MySQLdb, host='localhost', port=3306, db='test_db',
            user='root', passwd='passwd', mincached=5, charset='utf8')
db = Database(pdb.connection())
db.query('SELECT ...')
db.insert('INSERT INTO ...')
db.update('UPDATE ...')
db.delete('DELETE ...')
...

Otherwise, please use MySQLDatabase or SqliteDatabase to create a new connection.

Args:
connection:Specify an exists database connection.
kwargs:Connection parameters.
connect()[source]

Get this database connection

close()[source]

Closes this database connection

is_closed()[source]

Return if connnection is closed

iter(sql, *args, **kwargs)[source]

Returns an iterator for the given query and parameters.

query(sql, *args, **kwargs)[source]

Returns a row list for the given query and parameters.

get(sql, *args, **kwargs)[source]

Returns the (singular) row returned by the given query.

If the query has no results, returns None. If it has more than one result, raises an exception.

execute(sql, *args, **kwargs)[source]

Executes the given sql, returning the lastrowid.

rollback()[source]

Rolls backs the current transaction

execute_lastrowid(sql, *args, **kwargs)[source]

Executes the given sql, returning the lastrowid.

execute_rowcount(sql, *args, **kwargs)[source]

Executes the given query, returning the rowcount.

executemany(sql, args)[source]

Executes the given query against all the given param sequences.

executemany_lastrowid(sql, args)[source]

Executes the given query against all the given param sequences.

executemany_rowcount(sql, args)[source]

Executes the given query against all the given param sequences.

update(sql, *args, **kwargs)

Executes the given query, returning the rowcount.

delete(sql, *args, **kwargs)

Executes the given query, returning the rowcount.

updatemany(sql, args)

Executes the given query against all the given param sequences.

insert(sql, *args, **kwargs)

Executes the given sql, returning the lastrowid.

insertmany(sql, args)

Executes the given query against all the given param sequences.

class mandb.SqliteDatabase(db, *args, **kwargs)[source]

Subclass of Database, wrapper for Sqlite3

usage:

from mandb import SqliteDatabase

db = SqliteDatabase(db='test.db')
db.query('SELECT ...')
db.insert('INSERT INTO ...')
db.update('UPDATE ...')
db.delete('DELETE ...')
...
Args:
db:The sqlite database file.
class mandb.MySQLDatabase(*args, **kwargs)[source]

Subclass of Database, wrapper for MySQL

usage:

from mandb import MySQLDatabase

db = MySQLDatabase(host='localhost', port=3306,  db='test',
                   user='root', passwd='123456', charset='utf8')
db.query('SELECT ...')
db.insert('INSERT INTO ...')
db.update('UPDATE ...')
db.delete('DELETE ...')
...

Release history

Version 0.1.4, May 08 2017

  • Bug fix: Database._execute does not format sql by args when kwargs and args are empty.

Version 0.1, Mar 31 2017

  • Support MySQLdb, sqlite3 and DBUtils.
  • Make DBUtils support autocommit by default.

Indices and tables