Android SQLite ORDER BY query won't process - android

Ok, so I have a database of an accounting app, which contains the information of various items purchased.
Said database looks like this:
Unsorted SQLite database
Upon running the query SELECT * FROM items ORDER BY totalPrice DESC; in SQLite Studio 3.1.1, the db sorts itself cleanly to this:
Updated database
However, trying to run mDatabase.rawQuery("SELECT * FROM items ORDER BY totalPrice DESC;") via the Java SQLite library results in nothing. The db isn't sorted at all. I've also tried running mDatabase.query("items", null, null, null, null, null, "totalPrice DESC;");
I'm at a complete loss as to what to do for this problem. Any help is appreciated.

The command doesn't seem to go through, the database remains the same as the top picture.
A SELECT statement does not change the database. It returns data from the database. The Cursor that you get back from rawQuery() or query() will have the rows in its result set sorted per the ORDER BY clause. The actual database, though, will remain untouched.

Related

Inconsistent Android SQLite registers

I have a local SQLite database on my Android app. In order to perform some tests, I do some insertions with values.put(regValues); db.insert(register); db.close();. Then, I do some queries with the data and here it when it all gets messy:
The data is stored on a local SQLite database. When I query the data on my Android app with a general statement such as SELECT * FROM TABLE, it only retrieves 2 of the 8 inserted registers. This could be an insertion error, but here is the thing:
On Android, the query and the iteration is done the following way:
Cursor cursor = db.query(TABLE_NAME,
new String[]{TABLE_NAME.COLUMN_ID},
null,
null,
null, null, null, null);
cursor.moveToFirst();
int numberRegs = 0;
while(cursor.moveToNext()){
numberRegs++;
}
Log.d(LOG_TAG, "numberRegs: " + numberRegs);
When I check the registers with this web debugger, I can see all the 8 inserted registers. To complicate things a bit more, I have tried to download the database local on my computer and see if I could see the 8 registers inserted on the database, but I only see 4 of them.
Due to this, I am completely lost because I am not able to know the real state of the local SQLite database. Therefore, my questions are: why does this inconsistency happen and what can I do to fix it in order to see on my queries on Android the 8 registers instead of the current 2?
The problem was that using while(cursor.moveToNext()) stops before iterating through all registers. Instead of this, the iteration should be done like this:
for(int i=0; i<cursor.getCount(); i++){
cursor.moveToNext();
//Do stuff with cursor info
}

Select last row

Please how can I retrieve the last on an Android DB? I'm using the Sugar ORM library to abstract all db operation but I can't seem to be able to figure out how to retrieve the oldest record on the db.
You have to order by some timestamp, if you have it, or by the ID, if it is autoincrementing, and take only the first result:
Thing.find(Thing.class, null, null, null, "timestamp DESC", "1");
The basic algorithm is
Get a list of all objects from the database
Take the one with the "maximum" date
I'm unfamiliar with Sugar ORM, so I don't know how to do this with that particular library other than coding it myself. The SQL query for this would be fairly straight forward.

ContentProvider/ContentResolver Query Sorting by ID by default

I am working on an application that stores everything in a database and it is accessed using a ContentProvider. My scenario is as follows:
I make a web call and receive a list of integers which represent the ids of the objects I need to retrieve from my database on the device.
I make a call to ContentResolver.query() with the following Selection:
Selection: _id=? OR _id=? OR _id=?
Selection Ids: 30; 165; 149;
So, I need to get all items where the id is either 30, 165, or 149. And I need them in that exact order.
This is the exact call I am making on the ContentResolver:
Cursor cursor = mActivity.getContentResolver().query(myUri, null, selection, selectionIds, null);
As you can see, I do not pass in any sorting. However, the result gives me a Cursor with the order being the following: 30, 149, 165. So, it appears it is defaulting the sorting by _id even though I do not specify any sort order. My question is, does anyone know of a way to stop this from happening?
When you select rows, from any database, without specifying an ORDER BY clause, you should consider the order of the results as undefined, i.e. it could come back in any order. The reason you are seeing it sorted by _id here is just due to circumstance - they are likely to be in that order on the underlying database files so that is the order SQLite reads them back in. However it is not safe to assume that will always be so.
So the actual answer to your question is no, you can't assume SQLite will return your rows in any particular order without an ORDER BY clause. If you are unable to provide such a clause (which appears to be the case here) you'll have to sort them in code after getting all the data from the cursor.
It is not defaulting to _id, it is giving you the records as they are in the db (which happen to be sorted by id). Pass your own sorting order if you don't want this.

Getting unique rows from column sqlite android cursor

Hi I cant get unique rows tried this from the documentation:
public Cursor getcepaUnico(){
return database.query(true, "vino", new String[] {"_id", "cepa"}, null, null, null, null, "cepa", null);}
but shows duplicated rows even if the DISTINCT boolean is changed.
Also tried this:
public Cursor getCepaUnico() {
return database.rawQuery("select DISTINCT cepa from vinos", null);}
And the app crash after calling the method.
Setting distinct to true should have returned distinct results. Is it possible that your code which loops through the cursor is incorrect? You might want to post that also for review.
Regarding your rawQuery, you are using a different table name which is probably what is causing the crash. It should be "select DISTINCT cepa from vino" (not vinos) to match your query statement.
Not sure if this will solve your problem, but sometimes I just pull the db from the emulator (in the DDMS view in Eclipse) and run the query directly using an sqlite editor when my raw queries don't work; if the query shows what you want in the editor then use the query in the rawQuery method.
Firefox has a good sqlite editor.

choosing between requery or firing the query again in SQLite

i have been using a SQLite database, Theres a situation where i have a list which displays all the "name" field data of a table "table1". Now i have a button to insert data in "table1". The list is populated using a simple cursor adapter which is passed a cursor "cursor1" populated with the data. "cursor1" is prepared using the SQLite query - "SELECT * FROM table1". Now the moment i insert data, i need to update the list too.
My question is-
will the Adapter sense the database change automatically (i guess not)?
using cursor1.requery() is correct or should i use cursor1 = db.query("table1", null, null, null, null, null, null);
It would be helpful if you can throw some light on which 1 is better and in which situation. Coz for the situation which i explained above, the requery() command is not giving a valid result while the later 1 works fine. still cant understand what the problem could be.
will the Adapter sense the database
change automatically (i guess not)?
No, the Adapter will not sense the database change automatically.
using cursor1.requery() is correct or
should i use cursor1 =
db.query("table1", null, null, null,
null, null, null);
Use requery(). Here is a sample project from one of my books demonstrating the technique.

Categories

Resources