Would return the pet, it's owner and it's houdehold (assuming the relationships (pet->owener and pet->houdhold) are valid (in the above data they are not)) if either the pat name, the owner's name or the household name match the search item.Į.g. Val owner: = Household::class, parentColumn = "householdId", entit圜olumn = "id")Īnd a Query/Dao such as :- pet.* FROM pet JOIN owner ON owner.id = pet.ownerId JOIN household ON household.id = pet.householdId WHERE pet.name LIKE :search OR owner.name LIKE :search OR household.name LIKE :search")Ībstract fun getPetWithOwnerAndHousehold(search: String): List The result (for the same data) would be :- D/DBINFO: HOUSEHOLD>ID is h1 name is Davenport Close address is 1 Davenport Close region is region 1ĭ/DBINFO: OWNER>ID is owner1 Ownername is David address is 10 Somewhere Place telno is 1111111111ĭ/DBINFO: OWNER>ID is owner3 Ownername is Davina address is 3 Wayland Way telno is 333333333ĭ/DBINFO: PET>ID is pet1 Petname = Dave colour is brown height is 10 ownerID is Fred householId is 1 Maria CresentĪlthough according to the comment The example provided in the Android documentation seems to create a new combined model instead of returning separate entities from the 3 tables mentioned above using a POJO such as :- data class PetWithOwnerAndHousehold pet: entity = Owner::class, parentColumn = "ownerId",entit圜olumn = "id") id would then be able to identify the row from which the specific object could be obtained according to it's type.Īs an example the following code :- db = TheDatabase.getInstance(this)ĭao.insert(Pet("pet1","Dave","brown",10,"Fred","1 Maria Cresent"))ĭao.insert(Pet("pet2","George","red",11,"Bert","2 Somewhere Place"))ĭao.insert(Owner("owner1","David","10 Somewhere Place",1111111111))ĭao.insert(Owner("owner2","Laura","14 Overthere Way", 222222222))ĭao.insert(Owner("owner3","Davina","3 Wayland Way",333333333))ĭao.insert(Household("h1","Davenport Close","1 Davenport Close","region 1"))ĭao.insert(Household("h2","Daintree House","100 Smith Street","region 2"))įor(s: searchPOJO in dao.search("%dav%")) ".type is a literal used to identify from which table the data came.a UNION requires that number of columns match the initial select. If all you want is a LIST then you could use a custom POJO and a query that uses UNION's.įor example you could have a POJO such as :- data class searchPOJO(Īnd a Query such as:- id,'Pet' AS type,name FROM pet WHERE name LIKE :search UNION SELECT id, 'owner' AS type, name FROM owner WHERE name LIKE :search UNION SELECT id, 'household' AS type, name FROM household WHERE name LIKE :search")Ībstract fun search(search: String): ListĪnd you'd invoke it like:- dao.search("%dav%") Do you perform 3 separate queries for the 3 tables and combine them together further up the chain (e.g.Is there a way to do this with SQLite queries and a custom POJO?.What would be the best way of performing a query where it returns all entries in Pet, Owner and Household where name contains "Dav"Īnd get a list of data back similar to this (minus some fields for brevity) Pet("Dave".) // Pet Given that there are 3 tables class id: String,
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |