MySQL数据库包装器
mydb的Python项目详细描述
mydb
mydb是一个简单的mysql数据库包装器。
Install
pip install mydb
Usage
Single Connection
from mydb.connection import Connection db = Connection("mysql://youngking@localhost:13306/mydatabase") for article in db.query("SELECT * FROM articles"): print article.title
Multiple Connection Like Master/Slave
from mydb.router import ConnectionRouter from mydb.proxy import DBProxy router = ConnectionRouter(["myapp.somewhere.MasterSlaveRouter"]) db = DBProxy(router) for article in db.query("SELECT * FROM articles"): print article.title
Database routers
数据库路由器是一个类,它最多提供两种方法:
读取数据库(语句,**提示)
Suggest the database that should be used for read operations for ``SELECT`` statement. If a database operation is able to provide any additional information that might assist in selecting a database, it will be provided in the hints dictionary. Details on valid hints are provided below. Returns None if there is no suggestion.
db_for_write(型号,**提示)
Suggest the database that should be used for writes of statements except ``SELECT``. If a database operation is able to provide any additional information that might assist in selecting a database, it will be provided in the hints dictionary. Details on valid hints are provided below.
在tests/test_router.py中有一个示例。
路由器不必提供所有这些方法——它可能会忽略其中的一个或多个方法。如果省略其中一种方法, mydb在执行相关检查时将跳过该路由器。
在mydb.router中有一些默认路由器,您可以从中继承并覆盖它们。