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>
-
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
MySQLDatabaseorSqliteDatabaseto create a new connection.- Args:
connection: Specify an exists database connection. kwargs: Connection 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.
-
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 Sqlite3usage:
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 MySQLusage:
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._executedoes not format sql by args when kwargs and args are empty.
Version 0.1, Mar 31 2017¶
- Support
MySQLdb,sqlite3andDBUtils. - Make
DBUtilssupport autocommit by default.