Fetch Date from database that is shipped with android app - android

I am following this tutorial http://mobisys.in/blog/2012/01/tutorial-using-database-in-android-applications/ to have an external database with my android app.
I am not able to apply where clause while fetching the data from the database.
This is what I have done so far.
c=myDbHelper.query("level1", null, "_id=2", null, null,null, null);
if(c.moveToFirst())
{
do {
Toast.makeText(Level1.this,
"_id: " + c.getString(0) + "\n" +
"name: " + c.getString(1) + "\n"
,
Toast.LENGTH_LONG).show();
} while (c.moveToNext());
}
The databaseHelper class
public Cursor query(String table,String[] columns, String selection,String[] selectionArgs,String groupBy,String having,String orderBy){
return myDataBase.query("level1", null, "_id", null, null, null, null);
}
Can anyone help me with this?

You pass "_id=2" as selection when calling myDbHelper.query. But in your DB Helper query method, selection is never used (same goes for other parameters).
You probably want to change query to something like:
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy){
// double check parameters I did type that here (no IDE)
return myDataBase.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
}
See also, the documentation of SQLiteDatabase

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)

Android contentprovider complex query involving 2 tables

How do I run this query in a contentprovider?
SELECT * FROM sms_data WHERE number != (SELECT DISTINCT number from test) AND time > ?
First, the query is probably wrong. Instead of number != (...) you likely want number NOT IN (...).
Assuming test is a table in your app and not in the content provider, you can perform the query in two steps:
Subquery SELECT DISTINCT number FROM test. Build a comma-separated string such as '12345','23456','45678' from the results.
Do the content provider query with selection
number NOT IN (<that comma-separated list>) AND time > ?
For a subquery you can use rawQuery() or SQLiteQueryBuilder.
Overriding onQuery method of ContentProvider, I managed to put this bunch of code that works well for me.
#Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
switch (URI_MATCHER.match(uri)) {
case TABLE1:
Cursor cursor = database.query("sms_data", projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
case TABLE2:
Cursor c = database.query(true, "test", projection, selection, selectionArgs, null, null, sortOrder, null);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
case COMBINED:
String sql = "SELECT " + appendColumns(projection) +
" FROM sms_data WHERE number != IFNULL((SELECT DISTINCT number FROM test), 'abc') AND " + selection;
Cursor cur = database.rawQuery(sql, selectionArgs);
cur.setNotificationUri(getContext().getContentResolver(), uri);
return cur;
default:
throw new IllegalArgumentException("Unsupported URI: " + uri + " Match = " + URI_MATCHER.match(uri));
}
}
Hope this works for you too.

Android SQLite database remove duplicates from query

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)

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