Use Sqlite group_concat in db.query() instead of rawquery - android

What is the equivalent for db.query() of following query:
select group_concat(Id, ',') as ids from RECORD_MASTER
Its working fine with rawquery() but I want to implement it in query() function.

You may try this:
db.query(
DATABASE_TABLE,
new String [] {"group_concat(Id, ',') as ids"},
null, null, null, null, null
);
which corresponds to this function declaration:
Cursor query (String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy)

Related

Where clause is not working for long in android SQLite

I am trying for select statement in android SQLite
i am able to select all records in a cursor if i do below
String tableName = "Test";
String[] columns = {mobile};//mobile has a datatype number in db
Cusrsor c = m_sqlLiteDatabase.query(tableName, columns, null,null, null, null, null);
c.getCount(); // 11
but does not return any value if i add a conditon as below
long mobile = 123456;
m_sqlLiteDatabase.query(tableName, columns, "mobile=" + mobile ,null, null, null, null);
c.getCount(); // 0
is anything i am doing wrong?
how to use long
try to change your query code line with this
m_sqlLiteDatabase.query(tableName, columns, " mobile = ? ", new String[]{String.valueOf(mobile)} ,null, null, null);
Reference
query(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy)

How to put where condition and join in cursor Android

Hi i want to put a where condition and join tables in my query but i can't find any example
as for now i'm using this code, but i don't know where to put the join and where condition
public Cursor getUsers() {
Cursor localCursor =
this.myDataBase.query(TBL_INFO, new String[] {
KEY_ID,
KEY_NAME},
null,
null, null, null, null);
if (localCursor != null)
localCursor.moveToFirst();
return localCursor;
}
I'm wondering what's with the null value? is there any syntax that i can follow? thank you for the help!
db.query(tableName, columns, selection, selectionArgs, groupBy, having, orderBy);
You need to put your where condition in the selection part
Eg:
String selection="columnA=123";
For join you could try something like, tableName can be given tableName="table1,table2";
then your selection can have the conditions.(Have not tried this just theoritical)

adding DISTINCT keyword to query() with SQLite in Android

the difference between query() and rawQuery() in SQLite when making more complex SQL queries.
for example
i want to use the SQL keyword DISTINCT, so I don't get any duplicates returned from the database.
i understand how to use rawQuery() method, that way you can put an actual SQL query statement in the method. in this way i can make a standard SQL statement with rawQuery. it would be easy to add the DISTINCT keyword to any SQL statement when using rawQuery()
however, when using the query() method as shown here in this code, I can't just use regular SQL statements. in this case, how would i make a query with the DISTINCT keyword as part of the query? or something with the same functionality?
// get info from country table
public String[] getCountries(int numberOfRows) {
String[] columns = new String[]{COUNTRY_NAME};
String[] countries = new String[numberOfRows];
int counter = 0;
Cursor cursor = sqLiteDatabase.query(COUNTRY_TABLE, columns,
null, null, null, null, null);
if (cursor != null){
while(cursor.moveToNext()){
countries[counter++] = cursor.getString(cursor.getColumnIndex(COUNTRY_NAME));
}
}
return countries;
}
Instead of the...
public Cursor query(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy)
...method you're using, just use the...
public Cursor query (boolean distinct, String table, String[] columns,
String selection, String[] selectionArgs, String groupBy,
String having, String orderBy, String limit)
...overload and set distinct to true.
The Android docs seem a bit hard to direct link, but the doc page describing both is here.
you can use this,
Cursor cursor = db.query(true, YOUR_TABLE_NAME, new String[] { COLUMN1 ,COLUMN2, COLUMN_NAME_3 }, null, null, COLUMN2, null, null, null);
Here first parameter is used to set the DISTINCT value i.e if set to true it will return distinct column value.
and sixth parameter denotes column name which you want to GROUP BY.
You should use another QUERY function with first DISTINCT boolean parameter set to TRUE
public Cursor query (boolean distinct, String table,...)
this is the function i used in my app for getting distict name from a group table hope you get an idea ,have a look at it.only distinct values will be fetched if the column contains same names
public ArrayList<String> getGroupNames() {
ArrayList<String> groups = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String[] projection = {COLUMN_GROUP_NAME};
//select distinct values for group name from group table
Cursor cursor = db.query(true,GROUPS_TABLE_NAME, projection, null, null, COLUMN_GROUP_NAME, null, null,null);
if (cursor.moveToFirst()) {
do {
String group=cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_GROUP_NAME));
groups.add(group);
Log.d("group",group+"gp");
}while (cursor.moveToNext());
}
return groups;
}

How to get a cursor with distinct values only?

I want to return a cursor only with distinct values of a column.
The column 'Groups' has more items but with only 2 values: 1,2,1,1,1,2,2,2,1
String[] FROM = {Groups,_ID};
public Cursor getGroups(){
//......
return db.query(TABLE_NAME,FROM,null,null,null,null,null);
}
will return a cursor containing {1,2,1,1,1,2,2,2,1} but I would like to contain just {1,2}.
You can have an sql query like this,
public Cursor usingDistinct(String column_name) {
return db.rawQuery("select DISTINCT "+column_name+" from "+TBL_NAME, null);
}
you can use distinct argument while making query like this:
public Cursor query (boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
Follow this doc for more clearity.
You can use below example query, as you have to give column name for distinct
Cursor cursor = db.query(true, YOUR_TABLE_NAME, new String[] { COLUMN_NAME_1 ,COLUMN_NAME_2, COLUMN_NAME_3 }, null, null, COLUMN_NAME_2, null, null, null);
COLUMN_NAME_2 - name of the column for distinct.
remember to add GROUP BY column names
Use boolean true iin the distinct argument, For example :
public Cursor query (**true**, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit);

Query only the first data from a table

I'm trying to query only the first data from the table.
Cursor img_cursor = db.query(
IMAGE_URL_TABLE_NAME,
new String[]{"small_img_url" , "large_img_url"},
null",
null, null, null, null);
Could somebody tell me how to implement a query where only the first data is retrieved from a table?
Solution
I think I solved the answer:
Cursor img_cursor = db.query(
IMAGE_URL_TABLE_NAME,
new String[]{"small_img_url" , "large_img_url"},
null",
null, null, null, null , "1");
I used limit 1 first, but the application crashed. Only if I pass the number as a String will it work. The query has 8 parameters while we're using the limit parameter.
limit 1
From Documentation:
public Cursor query (boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
so, put your last argument to be "limit 1".
Cursor img_cursor = db.query(
IMAGE_URL_TABLE_NAME,
new String[]{"small_img_url" , "large_img_url"},
null,
null, null, null, "limit 1");

Categories

Resources