Room; Store integer array to a separate table? - android

Using Room ORM, I have declared an entity EQPreset using #Entity annotation. The entity contains an array int[]. It gives following error:
error: Cannot figure out how to save this field (int[] arr) into database. You can consider adding a type converter for it.
Normally saving EQPreset instance to a database, I would create a separate table to store values of the array and have a foreign key pointing to the relevant EQPreset.
However, I need to find what will be the way of storing this int[] arr of EQPreset using Room, that is, either by making a separate table or using any good approach/way.

Option #1: Get rid of int[] arr. Have some other entity represent this integer, with a foreign key back to the EQPreset entity. Have methods on your DAO be able to give you the integer-entities for a given EQPreset entity.
Option #2: Use a #TypeConverter to convert the int[] into something that can go into a single column (e.g., convert it to and from a JSON array, represented as a string).

Related

Room: Cannot use unbound fields in entities

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.

Android - Need to create database based on JSON object received from API response

I have an API from which I receive JSON object response, which is dynamic (meaning response JSON object contains different key-values time to time), so that the database for my Android application need to delete current table and create new table based on JSON keys.
For simple example, today I get JSON object response like
`{"name":"STRING", "age":"INTEGER"},
next week I will get response like
{"name":"STRING", "id":"INTEGER","Country":"STRING"}`
I checked Room and Realm, but the problem is that I can't able not get any clear examples or tutorials to make use of it.
FYI - I am new to Android database concept.
Edit #1:
What I need is to create DB Table columns based on JSON keys (i.e, by default table X has two columns A and B and receiving JSON object contains three keys A,B and C, so that DB need to include column C dynamically)
For storing values in Android Sqlite database
check his https://androidjson.com/save-store-php-mysql-json-parsing-data-sqlite/
it will be helpful to you

ORMLite auto-mapping of results to entity in custom query

I need to perform complex query with joins - seems to be too complex to use QueryBuilder, so I'll SQL my way around. I know however, that result of this query will be a list containing only a single entity type. Is there a way to leave mapping of results to ORMLite? Usually I parsed results myself, but in this case entity contains a lot of fields and I really don't want to go into parsing those from List<String[]>...
... that result of this query will be a list containing only a single entity type. Is there a way to leave mapping of results to ORMLite?
There certainly is. If you take a look at the "raw query" section of the documentation, you can see that it talks about the use of the RawRowMapper class. To quote from the docs:
You can also map the results into your own object by passing in a RawRowMapper object. This will call the mapping object with an array of strings and allow it to convert the strings into an object. The DAO provides a default RawRowMapper that can be gotten from orderDao.getRawRowMapper() that knows how to convert the string array into the object.
So for this you would call dao.queryRaw(...) with the RawRowMapper arg which maps from an array of strings. There are other dao.queryRaw(...) methods including:
queryRaw(String, DatabaseResultsMapper, String...) which allows you to map an object directly from the database results.
queryRaw(String, DataType[], RawRowObjectMapper, String...) which maps from an array of objects if you specify the result types.

Store custom objects (User defined obj) in SQLite Android database

How can I store my custom object in a database ? To be more clear how do I write these statements.
To create a table,
1.
CREATE TABLE IF NOT EXISTS MY_TABLE(
_id INTEGER PRIMARY KEY AUTOINCREMENT, customObject ??? NOT NULL);
What goes in the ????
To retrieve from a cursor,
2.
MyObject obj = cursor.get????
What method can I use to get my stored object.
I googled a lot, but no luck. Do I need to store a serializable object? how?
Thanks in advance.
How can I store my custom object in a database ?
You don't, unless you are using an object database. You store a representation of the object in a database.
What goes in the ????
That depends on what your representation is. If you convert the object to XML or JSON, it would be TEXT. If you convert the object to a byte array (e.g., Serializable), you would use BLOB.
What method can I use to get my stored object.
That depends on what your representation is. If you converted the object to XML or JSON, use getString(). If you converted the object to a byte array, use getBlob().
Most developers would not do any of this, but rather would store the attributes of the object as individual columns, to allow for the greatest possible flexibility in querying the database.

how to store the contents of an arraylist in one column of table in sqlite database in android?

I am having an arraylist of strings. I needt to store that arraylist in the 1 column of database.
How can I do that?
You can consider serializing an object into byte[] and saving it in your DB as a BLOB or going further encoding it in Base64 and saving as a TEXT. I'm not sure that any of these two are a good way to save your ArrayList - I'm just pointing options.
You might also consider creating other table and creating relation between them. There is a good pratice in databases called First Normal Form.
take a String, append arraylist's index string with the help of loop
String all="";
for(int i=0;i<arraylist.lenght;i++)
{
all=all+" "+ arraylist.get(i).tostring;
}
now store "all" in your DB field, only the problem with this is SIZE,
hope you got what i meant

Categories

Resources