Android SQLite database remove duplicates from query - android

I'm new to SQLite. I'm using this query in order to extract all rows from a column for a specific user:
Cursor c = db.query(true, TABLE, COLUMN, USER + "= '" + user + "'", null, null, null, null, null);
This is an example of table:
|group-----ID|
|------------|
|one-------me|
|one------you|
|two-------me|
|one-------me|
So the query for user "me" extracts "one, two, one". How can I filter the result in order to obtain a value just one time? Like "one, two"
Thanks

Use the Group By parameter with the appropriate column name:
Cursor c = db.query(true, TABLE, COLUMN, USER + "= '" + user + "'", null,
"group", null, null, null);
Since there are nine parameters in this method and three nearly identical methods...
public Cursor query(boolean distinct,
String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy, /* this one */
String having,
String orderBy,
String limit)

Related

how to filter multiple data on multiple column sqlite database

i want to filter multiple data such as
id = "1,3,5" from columnid which is having 1 to 10 id
and another column such as name
name = "a,e,d" from name column of 10 records
and another criteria such as age
age = "21,23,20" from age column of 10 records from same table,
one example i got is
Cursor cursor = db.query("TABLE_NAME",new String[]{"ColumnName"}, "ColumnName=?",new String[]{"value"}, null, null, null);
which is just for one column but i want to get data from multiple column, can anyone help me?
try this working example,
Cursor cursor =
db.query(TABLE_DIARYENTRIES,
new String[] {},
STUDENT_ID + " IN ("+resultStudent+")"+ " AND " +CLASS_NAME + " IN ("+resultClass+")"
+ " AND " +SUBJECT_NAME + " IN ("+resultSubject+")"
null, null, null, null);
and your result string should be 'a','b','c'
I really like the way Google's example is structured. Because for noobies such as myself it makes it really clear what I am doing. And it is also more robust to SQL injections. Here is my modified version of the Google example:
//Column(s) I want returned
String[] projection = {"ColumnIWantReturned"};
//Column(s) I want to filer on
String selection = "FilterColumn1 IN (?) and FilterColumn2 IN (?, ?)";
String[] selectionArgs = {"ArgumentForFilterColumn1", "FirstArgumentForFilterColumn2", "SecondArgumentForFilterColumn2"};
Cursor cursor = db.query(
"MyTable", // The table to query
projection, // The array of columns to return (pass null to get all)
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
null // The sort order
);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Log.d("this-is-a-test", cursor.getString(0));
cursor.moveToNext();
}
cursor.close();

Android SQLite: order by many columns

TABLE_NAME (COL1, COL2, COL3, COL4);
Cursor cur = db.query(TABLE_NAME, null, null, null, null, null, COL1);
This way we get the entries ordered by COL1 values. If some of them has the same COL1 value SQLite uses the same order as when they were stored by the previous insertions.
Is it possible to define many degrees of sorting? For example, if two or more entries has the same COL1 value then the one with the smaller COL2 value will go first and so on.
Android site says that we can specify only a String (column) for the sorting.
query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
If I use a
db.rawQuery("sql code")
the string of sql code will be compiled at runtime so the program will be slower. I would like to avoid that if not necessary. Are there any other ways?
With String orderBy you could specify multiple columns as this:
String order = COL1 + ", " +COL2 + ", " + COL3;
Cursor cur = db.query(TABLE_NAME, null, null, null, null, null, order);

SimpleAdapter needs an _id field

I got this :
Cursor c = db.query("Org", null, null, null, null, null, null);
which means I choose a table "Org", but together with this I need to make this :
Cursor c = db.rawQuery(" SELECT "+ id + " AS _id")
because SimpleAdapter need to have an _id field necessarily for some reason or it will crash with an error. How do I combine this 2 into one query?
The second parameter of the query function is the list of columns.
If you want to rename a column, you cannot just blindy return all columns but have to list the desired columns:
String[] columns = new String[] { id+" AS _id", "Name", "Color", "whatever..." };
Cursor c = db.query("Org", columns, null, null, null, null, null);
For your statement : Cursor c = db.query("Org", null, null, null, null, null, null); the second parameter is wrong, you shoukd mention the column names in it.
public Cursor query (boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
Whereas for,
Cursor c = db.rawQuery(" SELECT "+ id + " AS _id from Org");
means that you Select id and create an alias of it using AS into _id, and you are selecting this id from Org table.
so now you will be able to access the result from this query from the column name _id, and in order to access the result use:
c.moveToFirst();
while (c.moveToNext())
{
System.out.println(c.getString(c.getColumnIndex("_id"));
}

Cursor returns empty when querying for particular column values, even though rows exist

I have implemented a custom SQLite database helper. I have a table containing sms messages. I query this table for entries from a particular phone number like so:
public Cursor getSmsForContact(String phoneNumber) throws SQLException {
phoneNumber = PhoneNumberUtils.stripSeparators(phoneNumber);
Cursor cursor =
db.query(DATABASE_TABLE, new String[] {
KEY_DATE,
KEY_BODY,
KEY_CONTACT,
KEY_TYPE,
KEY_ROWID,
KEY_MESSAGE_STATE
}, KEY_CONTACT + "=" + phoneNumber, null, null, null, null);
return cursor;
}
This method works fine for numbers that do not have a "+" in the front (indicating a country code). However, it always returns an empty cursor when phoneNumber has a "+" in the front. I've verified that the PhoneNumberUtils.stripSeparators() method does not remove the "+" sign. I've also verified that rows exist in the table which match the phone number which is being queried.
Thanks!
Try
}, KEY_CONTACT + "= ?" , new String[] {phoneNumber}, null, null, null);
Phone number is being interpreted as a number; you want it to be interpreted as a string

data type mismatch error when using ORDER BY

I've got an android app using a local sqlite database.
private SQLiteDatabase mDb;
when I run this query I get my Cursor over rows with pid equal to id, as desired:
mDb.query(true, PT_TABLE, new String[] {KEY_PID, KEY_TID},
KEY_PID+" = "+id, null, null, null, null, null);
when I run the following query, aiming to get that same result set, ordered by pid I get "android.database.sqlite.SQLiteException: datatype mismatch"
mDb.query(true, PT_TABLE, new String[] {KEY_PID, KEY_TID},
KEY_PID+" = "+id, null, null, null, null, KEY_PID+" DESC");
Any ideas?
It looks like you got just a little mixed up. According to the SQLiteDatabase.query documentation, the last argument is the LIMIT clause. The second to last is the ORDER BY clause.
Cursor query (boolean distinct,
String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy, // <-- ORDER BY
String limit)
EDIT
But, there is also another SQLiteDatabase.query where ORDER BY would be last
Cursor query (String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy)
This worked for me
String filter = MySQLiteHelper.JOB_ID + "=" + Integer.toString(jobID);
String orderBy = MySQLiteHelper.LOG_TIME + " DESC";
Cursor cursor = database.query(MySQLiteHelper.LOG_TABLE_NAME, logTableColumns,
filter, null, null, null, orderBy);
KEY_PID + " = " + "'" + id + "'"
Since Orderby is the second last parameter in the query;
your query would be like this
mDb.query(true, PT_TABLE, new String[] {KEY_PID, KEY_TID},
KEY_PID+" = "+id, null, null, null, KEY_PID+" DESC", null);
If I correctly understood your problem, try this.
String[] columns = new String[] {KEY_PID, KEY_TID};
String where = KEY_PID + " = " + Integer.toString(id);
String orderBy = KEY_PID + " DESC";
Cursor cursor = mDb.query(PT_TABLE, columns, where, null, null, null, orderBy);

Categories

Resources