最近疯看全职猎人
初步学习了peewee,因为sqlalchemy实在是太重量级,文档也太恶心,peewee上手容易得多,也非常light
结合celery和peewee,写了点东西,查询年龄
myapp/db.py:
from __future__ import absolute_import
from myapp.celery import app
from peewee import *
db = SqliteDatabase('people.db')
class Person(Model):
name = CharField()
age = IntegerField()
class Meta:
database = db
@app.task
def agequery(name):
db.connect()
# collection = []
for i in Person.select().where(Person.name == name):
return {'name':name,'age':i.age}
# collection.append({'name':name,'age':i.age})
# return collection
db.close()
@app.task
def savedata(message):
db.connect()
newdata = Person.create(name=message.get('name'),age=message.get('age'))
newdata.save()
db.close()
@app.task
def initdb():
db.connect()
db.create_tables([Person,])
db.close()
测试用例:
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from myapp.db import savedata,agequery
>>> res = agequery('miaoting')
>>> res = agequery.delay('miaoting')
>>> res.get()
{u'age': 17, u'name': u'miaoting'}
>>> savedata({'name':'Jc','age':65})
>>> savedata.delay({'name':'Jc','age':65})
<AsyncResult: c6d06e50-42b3-42a1-b06b-2561eb0c64af>
>>> res = agequery.delay('Jc')
>>> res.get()
{u'age': 65, u'name': u'Jc'}
>>> savedata.delay({'name':'FanShu','age':99})
<AsyncResult: a348b074-a6f1-430c-af78-42d8e29cf4e6>
>>> res = agequery.delay('Fanshu')
>>> res.get()
>>> res = agequery.delay('Jc')
>>> res.get()
{u'age': 65, u'name': u'Jc'}
>>> res = agequery.delay('Fanshu')
>>> res.get()
>>> res = agequery.delay('FanShu')
>>> res.get()
第一次使用db时要用initdb初始化,除非删除sqlite数据库,否则只需调用savedata和agequery
peewee笔记:
peewee 学习:
在peewee中,一个继承model类的类定义为某个数据库的一个表格
类中的feild,为数据库的列(字段)
一个model类实例则是一行
example:
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.
class Pet(Model):
owner = ForeignKeyField(Person, related_name='pets')
name = CharField()
animal_type = CharField()
class Meta:
database = db # this model uses the "people.db" database
db.connect()
db.create_tables([Person, Pet])
保存数据:
>>> 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 database
1
>>> bob_kitty = Pet.create(owner=uncle_bob, name='Kitty', animal_type='cat')
>>> herb_fido = Pet.create(owner=herb, name='Fido', animal_type='dog')
>>> herb_mittens = Pet.create(owner=herb, name='Mittens', animal_type='cat')
>>> herb_mittens_jr = Pet.create(owner=herb, name='Mittens Jr', animal_type='cat')
删除数据:
>>> herb_mittens.delete_instance() # he had a great life
1
查询数据(根据类的feild访问相关字段的数据):
>>> 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 True
Grandma L. True
Herb False
比较两种查询的区别:
>>> query = Pet.select().where(Pet.animal_type == 'cat')
>>> for pet in query:
... print pet.name, pet.owner.name
...
Kitty Bob
Mittens Jr Herb
>>> query = (Pet
... .select(Pet, Person)
... .join(Person)
... .where(Pet.animal_type == 'cat'))
>>> for pet in query:
... print pet.name, pet.owner.name
...
Kitty Bob
Mittens Jr Herb
后者的效率更高,后者不做无谓查询
来源:oschina
链接:https://my.oschina.net/u/987833/blog/469161