sql order by clause - android

Ok so in my database I have,
1,2,3,4
But what I really want is,
2,1,4,3.
so how do I order a column against a predefined set of sequence of strings or values?

Create a order by column, and order the results based on that.

Related

Sqlite order by rows order

I am using this query
"select * from SomeTable group by SomeColumn"
It is returns list with accenting order, but i need to same order like in database.
For example the order in database is:
p
a
s
But result is:
a
i
p
Sample
The result need to be like distinct by CityEN but with all columns and order like 1.Paris 2.Amsterdam 3.Istanbul
In Sqlite, each row of a table has a unique rowid, which you can use for sorting.
select * from SomeTable group by SomeColumn order by rowid;
In your statement, add this line to sort the results:
order by min(rowid)
Your query does not enforce any order with ORDER BY clause so no assumption about row order should be made. If you want specific order add i.e. ORDER BY SomeColumn. See docs about all available order options: https://www.sqlite.org/lang_select.html#orderby
By the rules of SQL, you can't count on getting records back in any specific order without specifying an ORDER BY clause in your SQL query.
In practice servers sometimes return values in the order in which they're inserted, in the order of the first index created, or in the order of the primary key--but you can't count on this behavior, and in fact I've seen the behavior change between database maintenance windows or after the database version is upgraded. You definitely wouldn't want to count on a DB engine to give you back records in any particular order if you write a SELECT statement without an ORDER BY clause.
The only real way to get your records back in the order you inserted them is to create a timestamp column and then sort on it during the SELECT. If you don't want to worry about populating that column on INSERT, have that column auto-populate itself with a timestamp (depending on your DB engine).

How to retrieve data from more than one table in parse.com

I put my data in 3 tables(Links, Images and PDF)
each table has columns(university, faculty, grade, and description,...)
I want to retrieve description column in the 3 tables.
where university, faculty, and grade equal to certain values.
and sort them with creation date.
how can I perform that query in parse?
I'm not familiar with Android, but I'm pretty sure Parse does not support "Join" in the way a SQL database does. You could nest the queries, performing the next one in the previous one's completion block.
However, if you regularly want to get data from those 3 tables, I'd suggest you make it 1 table instead, with a column "Content" instead of Link/Img/PDF. Images and PDFs would probably be stored as PFFiles anyway, and you can put link as either its own string column or putting it in a file. You could also add a column "type" if you want to be able to query a specific type, or just keep track of which columns contains which data.
Then you could query the "Content" class, on the keys you want.
I think this link might help you
https://parse.com/docs/js/guide#relations and it is quite simple and nicely explained . You can't do it directly in the database, though.

Android SQLite: How to update one column of all records with different values

I found on Stack similar questions like this How to update an entire column in sqlite? But they don't explain me my how to solve my task.
Say, I have a db with 5 columns and 5 records in it. What i need is to update the last column "date" with the values of unix time that differs by 1 sec. So i need to put values 1406820974139, 1406820974140, 1406820974141, 1406820974142, 1406820974143, 1406820974144.
How to do it using ContentValues? As i got i have to loop five times to create new ContentValues object and update one record at a time (maybe using db.startTransaction() syntax).
My question is is there a way to put all values at a time into one ContentValues object and write in them into DB? Or maybe the better way is to use rawQuery using native SQL syntax as explained in How to update an entire column in sqlite?
In theory, it would be possible to put all the values into a single SQL statement:
UPDATE MyTable
SET date = CASE _id
WHEN 5 THEN 1406820974139
WHEN 17 THEN 1406820974140
WHEN 23 THEN 1406820974141
WHEN 69 THEN 1406820974142
WHEN 666 THEN 1406820974143
END;
However, just creating one ContentValues object for each row is easier than constructing this command.
so that we know which date should go to which row? what is the cririteria to differentiate the rows?
a relational db table is different from say an excel table. there is no implicit row order (if you always see the rows in the same order,you can consider it a kind of coincidence,you can not rely on this like you do in excel), in a db table you need to have a column(or a group of them) with unique values which you use in your queries to identify each of your records.
so you need to be more clear in your question. what date should go to which record (identified by what?). there is no implicit row number, if you want it, add an autoincrement PK column.
then you could for instance use something along the lines of
UPDATE table SET column5= 1406820974140+PKcolumn
where 1406820974140 is the start date you have to choose, depending on what you are up to

Is 'ORDER BY _id' enough to get rows in insertion order?

I'm using SQLite DB in my Android app.
Now I need to implement data extraction in the same order in which it was inserted.
Is it enough to do SELECT ... ORDER BY _id ... if the _id column is INTEGER PRIMARY KEY AUTOINCREMENT?
Or should I add a column to store the date and time a row has been created?
You can use ORDER BY _id. Make sure you are using a List and not a Set....
No need, an id generated by AUTOINCREMENT is enough.
Yes, if you just need the order, you may use such statement or just simple "SELECT ... FROM sometable". Results will be equals.

Is it possible to compare two cursors?

I need to check if two cursors pointing at the same rows with the same values. Is it possible?
More details:
I'm loading data from my own ContentProvider
I'm sending request to server and then updating my data inside ContentProvider with new values.
If values is changed - I need to notify user that he can update data.
As per CommonsWare's deleted answer:
Iterate over the relevant columns in the Cursor, retrieve the values, and compare each.
Although you may not know in advance the type of each column, you can find out with Cursor.getType(). You can also use Cursor.getColumnNames() to get the name of each column, and the number of columns.
This information will allow you to then use the correct accessor method to obtain each value and compare.
In SQLite, rows do not have an identity separate from their column values (but the ROWID is one of these values).
What you want requires that your data has some unique column(s) as part of the cursor, either the ROWID, or some other key value that is guaranteed to have no duplicates.
Otherwise, you can never know if what you see is just two records that happen to have the same values in those columns.

Categories

Resources