SQLite Query that parses JSON string in a column - android

I have a SQLite database built with ROOM persistence to store Users data.
One of the fields in the User object is a MAP which is stored in the table as Json String
column name: "user_status"
{"user1":1, "user2":0, "user3":2, ....}
Now I want to query through this JSON string to filter the list of users according to their status
Something like:
SELECT * FROM users WHERE status.user1 = 1
How can I manage that?

I managed to work around by using LIKE and parsing the JSON data as String
SELECT * FROM users WHERE status LIKE '%'||:userId||'\":1%' LIMIT 1
I know it might not be the best option, but works for now.

Related

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

Receive a specific data from fire-base real time database using android

I develop an android application for students attendance according to their seat numbers. If the student entered his seat number, it will be stored into real time database in fire-base.
Now, what I need to do is to retrieve the empty seat numbers to a list view according to students' information. for example: database has 50 seat numbers entered, but seat number (25) is not in the database then it will be retrieved to the list view.
In general how can I do that with fire-base real time database?
This is how database looks like for one student
You will need to mark seat empty in your db to later only fetch the empty seats. In current situation there is no way to put a query which can retrieve data only for absent students.
Your screenshot is limited so I suggest this structure.
{lectureId}/
seatNumber: {
isEmpty: boolean,
.... your other data
}
Then you will use orderByChild("isEmpty").equalTo(false);
And for querying a set of data from realtime database, have a look at the docs:
https://firebase.google.com/docs/database/web/lists-of-data
you can use a boolean if student is present (present = true).
put this variabble in te object
then you can run a loop on datasnashot for
i < noOfSeats
and show in te ListView with a condition
if (!present){
showInList();
}

get name from fom two table with foreignkey

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

Firebase matching substring

I have the JSON structure above
I want to get the list of vehicles where their ids ends with "123"
I had tried to use Query.endAt() method but i'm not sure if i'm using it right or it shouldn't give the required output
Query vehiclesRef;
vehiclesRef = db.getReference("vehicles").orderByKey().endAt("\uf8ff123");
Firebase Database queries can only perform prefix matches, so strings starting with a specific string or starting with a range of string. It is not possible to query for strings ending with a specific value, nor those ending with a range of values.
If you really want to do this within Firebase, consider also storing the reversed strings in the database:
"321_1_0"
"321_3_0"
"654_52_0"
Then you can query for strings starting with 321 with
vehiclesQuery = db.getReference("vehicles").orderByKey().startAt("321").endAt("321\uf8ff");

Compare a list of String values Firebase RealTime Database

Lets assume I have a List of String (ex: List<String>). I want to match all the Strings in the List to a particular attribute already present in the Firebase Database. How do I achieve this, as the equalTo() query only accepts a single String. I also don't want to generate as many queries as the List size. Any help would be much appreciated.

Categories

Resources