Getting unique rows from column sqlite android cursor - android

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.

Related

SQLite database Where clause not working for text datatype Android

I want to write a Where clause for Sqlite db & my query is as follows,
Cursor cursor = database.query(table_name,new String[]{COLUMN_1,COLUMN_2,COLUMN_3}, COLUMN_1='1', null, null, null, null);
//COLUMN_1='1' is my WHERE Clause & its datatype is text
I am not able to execute this query & its giving Nullpointer exception immediately after this statement.
I dont know the reason I think there is some problem with text datatype.
I have spent almost half a day searching for the solution but disappointed.
PS: I've also tried using ,
Cursor cursor = database.query(table_name,new String[]{COLUMN_1,COLUMN_2,COLUMN_3}, COLUMN_1=?, new String[] {'1'}, null, null, null);
LogCat:
But Same problem.
Do you initialize the database? Do you have anything like this:
database = new DBAdapter(this);
Also you need to open the database before the query.
database.open();
I have found the problem, the database was not opened & hence giving NPE. Thank you guys for your patience.

How to print query executed by query() method of android

We pass so many parameters to query() method of android. Android has simplified our work by giving query method. Is there any way so that i can print the sql query which android will form from this query method arguments which android creates and sends to sqlite.
According to a previous post, I have tried and I got the following solution in order to print the query string in the log.
Use buildQueryString method of SQLiteQueryBuilder. It takes almost same parameters as query() method takes .........
String sqlQry = SQLiteQueryBuilder.buildQueryString(false,TABLE_NAME, null, COLUMN_NAME_PATIENTID +"="+ patientID, null, null, COLUMN_NAME_PATIENTFIRSTNAME, null);
Log.i(TAG, sqlQry);
cursor = db.query(TABLE_NAME, null, COLUMN_NAME_PATIENTID +"="+ patientID, null, null, null, COLUMN_NAME_PATIENTFIRSTNAME);
For what it's worth, if you run under the debugger you can view the private member variable mQuery, which shows you the exact SQL query executed on that cursor - it's handy and can be used on demand without mucking with any code.
Since the query methods are of cursor type, I am not sure whether It will be printed or not.
If you want to debug any query, you can use EXPLAIN QUERY PLAN keyword along with the query or use SQLiteQueryBuilder() or simply run the SQL query by using rawQuery() method.
You can take references from:
http://developer.android.com/reference/android/database/sqlite/SQLiteQueryBuilder.html
http://www.sqlite.org/eqp.html

Default order of records returned by cursor was not same for mobile & tablet

I wrote a select query to access set of records from database by setting null for the argument 'orderBy' in the query(). I found that order of records returned by query() method when I run the application in mobile is completely different when I run the same sample application in tablet.
My Query:
Cursor cursor = database.query(true, tableName, downloadQueueTableColumnNames, selection, null, null, null, null, null);
Here, in the query orderBy field is null.
I hope someone to explain the reason behind this...
If you aren't ordering the results, they can be returned in any order. The same device doesn't have to give the same order if you call it twice in a row. If you want it in the same order every time, you must use an order by.

Queue SUM of SQLite column - android

I have a listview populated from an SQLite database. I have several items that I successfully populate into the listview, however I'm having trouble with one last thing.
I'm trying to queue the sum total of the column KEY_CONTENT6 which is a string type, however it only contains numbers. I'd like to keep it as a string, so to add it up I'm using Double.valueOf(). The problem is this code force closes on queue and I cant figure out whats wrong:
public Cursor queueAll(){
String[] columns =
new String[]{KEY_ID, "sum("+ Double.valueOf(KEY_CONTENT6) +")",
KEY_CONTENT9, KEY_CONTENT10 };
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
null , null, KEY_CONTENT10, null, KEY_CONTENT9+ " DESC");
return cursor;
}
simply use SUM, no need to use anything else..
String[] columns =
new String[]{KEY_ID, "sum(KEY_CONTENT6)",
KEY_CONTENT9, KEY_CONTENT10 };
It is valid for SQLite. Because, no matter what you set data type in SQLite, it stores values as string. So, type conversion is somewhat built-in in SQLite.
You can't use java in a SQL statement, either stick to strait sql or iterate over the cursor and use java to do your calculation.
You can find everything there is to know about sqlite here http://www.sqlite.org/docs.html
SQLite is basically typeless, so you might be able to use SUM on your column even though it is a string. However, if it's meant to be a numeric column, why not give it a number type??

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