web2py:检查数据库中是否有记录?

时间:2015-08-06 16:54:51

标签: database web2py

示例表:

db.define_table('pet',
    Field('name'),
    Field('owner_id','reference person'),
    Field('location_id','reference location'))

1)如何检查位于"Furry"的人15拥有的宠物26是否有?

2)如何检查位置8中是否有76的宠物?如果是这样,请退回。

1 个答案:

答案 0 :(得分:0)

问题1

db((db.pet.name == 'Furry') & (db.pet.owner_id == 15) & (db.pet.location_id == 26)).count()

问题2

db((db.pet.owner_id == 8) & (db.pet.location_id == 76)).count()

# if you have the name of the owner and the location you can even do 

db((db.pet.owner_id == 
    db(db.person.name == 'NO_8_PERSON_NAME'
       ).select(db.persone.id).first().id) & 
   (db.pet.location_id == 
    db(db.location.location_name == 'NO_76_LOCATION_NAME'
       ).select(db.location.id).first().id)).count()