I am using Room Database for storing chat, I have one group table and I want to fetch three parameters like this query
My Group Entity class is GroupUserEntity.kt
#Query("SELECT DISTINCT group_id, group_name, group_image FROM group_user_table")
fun getAllGroups() : List<ENTITY_CLASS>
I am not able to use my Group Entity class(GroupUserEntity.kt) over ENTITY_CLASS, forcefully I have to make new Entity class(Pojo Class) to fetch those three values.
So my questions is, there is any way to utilize my old Entity class(GroupUserEntity.kt) to fetch all groups? or I need to make a new POJO class for that?
It depends on number of variables in your GroupUserEntity.kt.
If for example you have group_id, group_name, group_image and group_category (total 4) in GroupUserEntity.kt and you are fetching data for only 3 variables then it will show you an error. (Cursor mismatch).
To avoid this either call ALL the columns values from database to match GroupUserEntity.kt, or create a new POJO.
Related
I've found literally 0 articles/threads about this error on the internet so I'm absolutely clueless, so here it goes.
I have a class A and a class B.
class A is having an ArrayList of class B instances like this:
#Entity
class A {
#PrimaryKey
var id = 0
var listOfB: ArrayList<B>
}
And here it goes the B class:
#Entity
class B {
#PrimaryKey
var id = 0
var specificDate: Date? = null
}
Whenever I try to run the project I get the following error message:
Cannot use unbound fields in entities.
Which points exactly to the ArrayList of B instances inside the A class.
What may be causing this?
An Entity has two purposes:
It defines the columns of a table in the database.
An instance of the entity represents one row of an element in that database, which can be given to you as a query result, or you can pass to Room to add or edit a row in the corresponding table.
SQL/SQLite doesn't have the concept of a table column where the type is another table or list of rows from another table, so what you're defining doesn't make sense. (Incidentally, you cannot use Lists of any type for a column type in a table. You can only use primitive type classes in an Entity class since that's all that SQL supports.)
Instead, you must define a relationship between your two tables. In this case, you can remove the list property from your class A. I'm not sure if you're trying to define a one-to-many or many-to-many relationship, but you can look at the Room documentation here for an example of how to set up relationships.
Basically, if a B can only be a member of a single A at a time, B should reference an A row ID in one of its columns, and you have a one-to-many relationship. Room will let you create a third data class (not an entity) to define the relationship in a natural way that looks similar to your example class A and can be returned in queries.
If B can belong to multiple different A's, then you have a many-to-many relationship, which is more complicated. A third table (Entity class) has to be defined to link the relationships together.
I need to migrate from Sqlite database to room in my application, but before migrating i have some doubts regrading room database i have searched through many sites but i have not satisfied with the solution.
My table schema will be changing frequently through API calls, is it possible in room ?
We need entities to map Column Name with entity properties, Since my table schema changes frequently how do i create the entity ?
Is it possible to map the multiple columns to single field property in entity, like say
Table A has three columns COL_A,COL_B,COL_C
#Entity
class TableA{
#ColumnInfo("COL_A")
val columnA : String
#ColumnInfo("COL_B,COL_C")
val columns : Map<String,String>
}
Is it possible to create entity like above in room?
Can anyone clear the above doubts ?
Table -article-
id |nam|image|id_categor(FK)|
table category
id |categorieName|
I have two tables in the database. The first table has a foreign key to the second table. Can I get name from the second table by the foreign key ?
need a JSON like
"categories":[
{"categorieName":"test1",
"data":[
{
"name":"wallpaper1",
"image":"tv_101.jpg"
},
{
"name":"wallpaper2",
"image":"tv_102.jpg"
}
]
Welcome, You need to provide a better description with a better code so everyone can understand easily.
If I understand your problem properly then you need a join. You can use SQLite rawQuery to get the desired output.
For Example:
private final String JOIN_QUERY = "SELECT * FROM article a INNER JOIN categories b ON a.id=b.other_id WHERE b.property_id=?";
db.rawQuery(MY_QUERY, new String[]{String.valueOf(propertyId)});
EDIT: I still don't get your question but I think you are using PHP
First of all you cannot directly get the JSON from the database. What you need to do is:
Query your required data using PHP
SELECT * FROM categories then loop it in php and get all articles in each category by querying SELECT * FROM articles WHERE cate_id=loopCategoy_Id
And save it in a key value array
Then convert the array into JSON
Send JSON response
Did you try join ?
Joining two tables by common columns will definitely help here
So I have two tables City and Street I wrote query to return City and its streets with one query response looks like this
1 City1
1 Street1
2 Street2
3 Street3
and I have POJO class for this response:
data class CityResponse(var city: City, var streets: List<Street>)
But I can't compile because it throws error:
error: Cannot figure out how to read this field from cursor private java.util.List<Street> streets
How can I write this response data class that room would understand what do I mean with variable streets: List<Street>
In this case used joins query and make one common pojo class that give both details. then make query like below ..
#Query("SELECT * FROM Student s LEFT JOIN BookIssue b ON s.StudentId =b.StudentId")
List<JoinData> getJoinData();
you can also implement inner join and others joins but make one single pojo that give both table record.
and also one more way first find city and make for loop and find based on city name into street and getting street data.
this is a example:
User and Orders. 1:N relation
but UserDAO do not have a method called "setOrders(List...)"
only getOrdersList() and resetXXXList()
You have to do it from the other part of the relation, Orders, using something like order.setUser(user) and updating the entity in the DB.