peewee是一个轻量级的pythonORM库。内建对SQLite、MySQL和PostgreSQL的支持。支持Python2.6+和Python3.2+。
pip安装:pipinstallpeewee
示例代码:
from peewee import *db = SqliteDatabase('people.db')class Person(Model): name = CharField() birthday = DateField() is_relative = BooleanField() class Meta: database = db # This model uses the "people.db" database.>>> from datetime import date>>> uncle_bob = Person(name='Bob', birthday=date(1960, 1, 15), is_relative=True)>>> uncle_bob.save() # bob is now stored in the database1>>> grandma = Person.select().where(Person.name == 'Grandma L.').get()>>> grandma = Person.get(Person.name == 'Grandma L.')>>> for person in Person.select():... print person.name, person.is_relative...Bob TrueGrandma L. TrueHerb False高级用法:
import peeweefrom peewee import *db = MySQLDatabase('jonhydb', user='john',passwd='megajonhy')class Book(peewee.Model): author = peewee.CharField() title = peewee.TextField() class Meta: database = dbBook.create_table()book = Book(author="me", title='Peewee is cool')book.save()for book in Book.filter(author="me"): print book.titlePeewee is cool
评论