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....
Related
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
recently i've started programming on android sqlite and I was debugging my sql commands with Mysql Workbench.
In one of my commands I've got this :
"Select itens.name_item , itens.pot_item from region,itens,item_regions where region.cod_region = "+id+" and itens.cod_item = item_region.cod_item;"
basically I have 3 tables.
region, itens and uniting with foreign keys those 2 inside of item_region.
What i want is by already having the region code, get the name and power of the registered itens in item_regions.
Sry if it's a little confusing, i'm very rusted on my english
Do an inner join:
SELECT name_item, pot_item FROM items
INNER JOIN item_region ON items.cod_item = item_region.cod_item
WHERE item_region.cod_region = ?;
I assume from your example you have the region code in the item_region table, otherwise you'll have to join in the region table as well to compare to the id.
Also be careful that the value of the variable id is sanitized so you don't expose your application to SQL injection.
Please excuse me if its a repeated question. I tried searching here and at google but I couldn't exactly find what I wanted.
I have got two tables A & B.
Table A Fields : id, name, description, rating.
Table B Fields : id, aId (linked to table A), customerId, recommended.
Table A contains my data items for which I'm storing average cumulative ratings provided by users.
Table B stores another attribute for data of Table A. It stores the recommended bit (1 for recommended & 0 for non-recommended).
I want to list all the data from Table A but I want to sort them using recommended bit from Table B. So, if there are 10 records in Table A and 2 records in Table B, while listing all those 10 records, the two from Table B should come first and then the others from Table A. It doesn't matter whether the recommended bit value is a 0 or a 1. While listing the other 8 records from Table A, I want to list the records based on their rating in descending order.
Can someone please guide me in writing this sqlite query for Android app? Thanks in advance!
The left join adds the recommended field to the result set (with a value of NULL if there is no matching B record).
The expression recommended IS NULL or EXISTS(...) returns either 0 or 1:
SELECT DISTINCT A.*
FROM A LEFT JOIN B ON A.id = B.aId
ORDER BY B.recommended IS NULL,
A.rating DESC
Alternatively:
SELECT *
FROM A
ORDER BY NOT EXISTS (SELECT 1
FROM B
WHERE B.aId = A.id),
rating DESC
I have a database with tables Teachers,Subjects and Teachers_Subjects in my android sqlite database.My tables have structure as shown in the image.
I need to query tables to get all subjects rows that are related to a single teacher. Initially I have the _id of the teacher.Using teachers _id I need to find the subjects.According to me first I need to find all the rows in Teachers_Subjects Table related to a Teacher and then make other query using the resulted rows and Subjects Table with JOIN statement to get all rows related to that teacher.I wanted to know is there any better way/query to accomplish this?If not then what should be the query for the solution mentioned above?
SELECT Subjects.*
FROM Teachers_Subjects JOIN Subjects
ON Teachers_Subjects.subject_id = Subjects._id
WHERE Teachers_Subjects.teacher_id = ?
In my Android app, I need to temporarily store some data in a form of table such as follows:
id | column 1 | column 2 | ... | column n
The data are downloaded from a server whenever users press a button. However, the data table doesn't have a fix number of column (as well as row) every time user downloads it from the server. For example, the server may send data with 3 columns the first time. Then it might send data with 5 columns the second time, etc...
Given this scenario, I think the database is probably the right data structure to use. My plan is to create a database, then add and delete tables as necessary. So I have been reading various tutorials on Android database (one example is this one http://www.codeproject.com/Articles/119293/Using-SQLite-Database-with-Android#). It seems to me I cannot create new table with variable number of columns using the sqlite database. Is this correct? In the onCreate(SQLiteDatabase db) method, the "create table" command must be specified with known number of columns and their data types. I could provide several "create table" commands, each with different number of columns but that seems like very crude. Is there a way to create database tables with variable number of columns on the fly?
Another alternative probably using several hash tables, each storing one column of the data table. I'm seriously considering this approach if the database approach is not possible. Any better suggestion is welcomed.
There is no such thing as a variable number of columns in an SQLite data base. Also, adding and deleting tables dynamically seems like a horrible hack.
It sounds like you want to store an array of values associated with an id. I suggest you think in terms of rows, not columns. Use a table structure like (id, index, value); each array of values returned by the server results in as many rows as necessary to store the values.