python mysqldb的一个非常简单的包装器,具有非常基本的功能
simplemysql的Python项目详细描述
python mysqldb的超简单包装,具有非常基本的功能
http://nadh.in/code/simplemysql
许可证:GPL v2
安装
pip install simplemysql
用法
from simplemysql import SimpleMysql db = SimpleMysql( host="localhost", db="mydatabase", user="username", passwd="password" ) # insert a record to the *books* table db.insert("books", {"type": "paperback", "name": "Time Machine", "price": 5.55, year: "1997"}) book = db.getOne("books", ["name"], ["year = 1997"]) print "The book's name is " + book.name
查询方法
insert(),update(),delete(),getone(),getall(),query()
插入(表,记录{})
将单个记录插入表中。
db.insert("food", {"type": "fruit", "name": "Apple", "color": "red"}) db.insert("books", {"type": "paperback", "name": "Time Machine", "price": 5.55})
更新(表,行{},条件[])
基于条件(或无条件)更新一个或多个行。
# update all rows db.update("books", {"discount": 0}) # update rows based on a simple hardcoded condition db.update("books", {"discount": 10}, ["id=1"] ) # update rows based on a parametrized condition db.update("books", {"discount": 10}, ("id=%s AND year=%s", [id, year]) )
insertorrupdate(表,行{},键)
插入新行,或在主键冲突时更新。
# insert a book with id 123. if it already exists, update values db.insert("books", {"id": 123, type": "paperback", "name": "Time Machine", "price": 5.55}, "id" )
getone(表、字段[]、条件[]、顺序[]、限制[])
getall(表、字段[]、条件[]、顺序[]、限制[])
从给定条件(或无条件)的表中获取一条或多条记录。结果行作为namedtuples返回。getOne()返回单个namedtuples,getAll()返回namedtuples的列表。
book = db.getOne("books", ["id", "name"]) # get a row based on a simple hardcoded condition book = db.getOne("books", ["name", "year"], ("id=1")) # get a row based on a simple hardcoded condition book = db.getOne("books", ["name", "year"], ("id=1")) # get multiple rows based on a parametrized condition books = db.getAll("books", ["id", "name"], ("year > %s and price < 15", [year, 12.99]) ) # get multiple rows based on a parametrized condition with an order and limit specified books = db.getAll("books", ["id", "name", "year"], ("year > %s and price < 15", [year, 12.99]), ["year", "DESC"], # ORDER BY year DESC [0, 10] # LIMIT 0, 10 )
lastID()
获取最后一个insert id
删除(表、字段[]、条件[]、顺序[]、限制[])
根据条件(或无条件)删除一个或多个记录
# delete all rows db.delete("books") # delete rows based on a condition db.delete("books", ("price > %s AND year < %s", [25, 1999]))
查询(表)
运行原始SQL查询。返回mysqldb游标。
db.query("DELETE FROM books WHERE year > 2005")