get name from fom two table with foreignkey - android

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

Related

SQLite Query that parses JSON string in a column

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.

Android upload SQLite data to server database

Here are my tables from SQLite :
Table1 - QUIZ
quiz_id
quiz_name
quiz_dateofcreation
quiz_key
Table2 - QUESTION
question_id
question_name
question_type (it could be S_answer or MCQ_answer)
quiz_id_fk
Table3 - S_ANSWER (standard answer)
s_answer_id
s_answer_name
question_id_fk
Table4 - MCQ_ANSWER (multiple choice answer)
mcq_answer_id
mcq_answer_name
mcq_answer_type (it could be correct or wrong)
question_id_fk
I want to send data to server for one QUIZ .Example when user click on send quiz data ,it wil be sended. DB on server is similiar, additional table for user.
First problem for me is SQL statement to get all data per QUIZ...
Second problem for me is best way to send data, i suppose
first data retrieved with sql convert to JSON like Convert SQLite to JSON
and then send them with help android volley class
If there is best way .. please let me now...
select
QUIZ.quiz_name
,QUESTION.question_name
,ANSWER.answer_name
,MCQ_ANSWER.mcq_answer_name
from QUIZ
left join QUESTION on QUESTION.quiz_id_fk = QUIZ.quiz_id
left join ANSWER on ANSWER.question_id_fk = QUESTION.question_id
left join MCQ_ANSWER on MCQ_ANSWER.question_id_fk = QUESTION.question_id
//where QUIZ.quiz_id = ...
This query will join all the tables you need so you can select any data you wish for any table. I'm not sure how you will be passing the quiz_id but where is the where clause you would use. I'm not sure how your data works so I used left joins but you can alter to your needs. I have only selected a few columns from the tables but you get the point....

Android - Parse Query - need to know if a string is contained within one of multiple columns in my database table

I have an Android application using parse as a backend. One of my tables in my parse database contains groups of users, one row in this table contains 6 columns of users to form one group.
I would like to return each row where the current user is contained in one of the columns.
My question is what is the most optimum way of achieving this?As it stands I can think of two ways:1. query each row in the groups table and loop through each cloumn looking for the current users username.
2. if possible - use a parse query that says if currenUser equals member 1 or member 2 or member 3 or member 4 or member 5 or member 6. But I don't know if this is possible
Any pointers or help would be appreciated?
For your ParseQuery<ParseObject> query object a whereEqualTo method is available query.whereEqualTo(key, value); try using your column name in place or key and your desired value in in place of value.
Using this you will only get the rows which contains your desired values. It just works as the where clause in SQL qyery. so you will not need to iterate over all the DB entries.

How to sort an array and then save to database in Android

I'm trying to sort an array of numbers, then take the output to save it to my database. Any help would be appreciated, thank! I can sometimes get the sort to work, but I'm having trouble accessing the array to get specific numbers to then save.
Integer[] toSortArray = {saveInt1, saveInt2, saveInt3, saveInt4, saveInt5};
Arrays.toString(new Integer[]{toSortArray[2]})
to sort, just do
Arrays.sort(toSortArray);
You can store data to database in any order but you can retrieve those in sorted order using query below.
"SELECT * from TABLE_NAME ORDER BY COL_NAME ASC/DESC;"

How to Perform the following query using Greendao ?

I have 2 tables A and B.
Table A contains names and table B contains selected names.
Now I would like to perform the following query on these tables using greendao, Please let me know if it is possible and if it is not are there any alternatives (maybe a raw query).
select *
from A inner join B
on A.nameid = B.nameid
Also, Table A columns: id, nameid, name
and Table B columns: id, nameid, name, rating
I think this might help.
You can use the raw query as a fake join. And you get all you want in the Query object
Query query = ATableDao.queryBuilder().where(
new StringCondition("nameid IN " +
"(SELECT nameid FROM B_Table )").build();
Since "nameid" doesn't seems to be a unique identifier in your sample. I won't suggest to use Relations to solve this issue. If you are try to use Relations, you can find my previous answer here.
Try this:
List<ATableObj> listATableObj = ATableDao.queryRawCreate(", BTable BT
WHERE BT.nameid = T.nameid").list();
If you use greendao this works differntly:
Instead of your query you select rows from table a (or b) and if you need a field of b (or a) you call getB() (or getA()) to get the corresponding row of that table.
If you have rows in table a that have no match in table b and you have rows in table b that have no match in a and you onlly want to select everything that has matches uin both tables, you would have to do a raw query to filter the rows of a (or b).

Categories

Resources