How to use paging in android sqlite? any example code directly in java?
I am using
Cursor cursor = mDb.query(DATABASE_TABLE, new String[] {KEY_DATE, KEY_REPS,
KEY_WEIGHT}, null, null, null, null, KEY_DATE + "DESC", ???);
Here there is a similar question/answer Limit Records fetched in Android (Sqlite database)
Some of the suggestions there are to use LIMIT ... OFFSET in your query, or to create your custom Cursor Adaptor. I guess it depends in which level you want to do the paging.
You can use the LIMIT keyword with index and no. of rows to fetch certain records, it goes something like this:
Cursor cursor = mDb.query(DATABASE_TABLE, new String[] {KEY_DATE, KEY_REPS,
KEY_WEIGHT}, null, null, null, null, KEY_DATE + "DESC LIMIT index, No. of records to fetch", ???);
Hope this helps.
Related
I want to know how to use query method that have where clause with like property.
basically what i want is to select all the columns WHERE C_NAME column is LIKE keyWord.
I've tried this, but it doesn't work:
cursor = db.query(TABLE, null, C_NAME, new String[] {"like '"+keyWord+"'"}, null, null, null);
Look at the docs. They say:
selection A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table.
so try this:
cursor = db.query(TABLE, C_NAME, new String[] {C_NAME + " like '" + keyWord + "'"}, null, null, null);
Also see this answer as well to see some examples on how to use the selectionArgs (4th argument above). That is where keyWord would have to go.
this query work perfect for me
Cursor cursor = db.query(true, TABLE_NAME, new String[]{ COLUMNS }, ROW + " LIKE ?", new > String[] { "%" + name + "%" }, null, null, null, null);
How do I use the String[] selectionArgs in SQLiteDatabase.query()? I wish I could just set it to null, as I have no use for it. I am just trying to load an entire unsorted table from a database into a Cursor. Any suggestions on how to achieve this?
selectionArgs replace any question marks in the selection string.
for example:
String[] args = { "first string", "second#string.com" };
Cursor cursor = db.query("TABLE_NAME", null, "name=? AND email=?", args, null);
as for your question - you can use null
Yes, you may set all parameters to null except the table name.
for example:
Cursor cursor = db.query("TABLE_NAME", null, null, null, null, null, null);
I am currently working on an Android project in Eclipse and i am having problems with my SQL query.
I am trying to order the query by more than two columns, currently i am doing it by KEY_DAY_ID but i want to also do it by KEY_START_TIME, but i can't get it to work
my query currently looks like this:
Cursor cursor = db.query(TABLE_SESSION, new String[] {KEY_ID, KEY_MODULE_CODE,
KEY_DAY_OF_WEEK, KEY_START_TIME, KEY_DURATION, KEY_SESSION_TYPE, KEY_ROOM},
null, null, null, null, KEY_DAY_ID + " ASC");
Please let me know your thoughts. Thank you in advance!
The last parameter in db.query() method is the order by clause (without the "order by"). All you need to do is separate both columns by a ",". So it would look like:
Cursor cursor = db.query(TABLE_SESSION, new String[] {KEY_ID, KEY_MODULE_CODE,
KEY_DAY_OF_WEEK, KEY_START_TIME, KEY_DURATION, KEY_SESSION_TYPE, KEY_ROOM},
null, null, null, null, KEY_DAY_ID + " ASC, " + KEY_START_TIME + " ASC");
This works for me
SQLiteCursor cursor = (SQLiteCursor) db.query(DbHelper.TIMES, colmn, null, null, null, null, DbHelper.TABLE_DAY + " ASC, " + DbHelper.TABLE_LECTURE_NO + " ASC",null);
Also you can do it in select line like this:
Cursor data = ddbb.rawQuery("SELECT * FROM vacations ORDER BY NAME ,MONTH , date ",null);
in previous code the first probability for the first column "NAME" then will start arrange by the Second probability "MONTH" then the third "date".....
which mean working in series
Or:
Cursor data = ddbb.rawQuery("select * from vacations where NAME = ? ORDER BY MONTH AND date ",new String[]{ns});
in previous code by using "AND" the two conditions are working together in parallel
I have variable:
String owner="Mike";
String[] columns ={"quantity", "price","owner"}
My cursor is trying to get
Cursor findEntry = db.query("sku_table", columns, "owner="+owner, null, null, null, null);
I got an error no such column error
android.database.sqlite.SQLiteException: no such column: owner: , while compiling: SELECT quantity, price, owner, FROM sku_table WHERE owner=Mike
But if I take this query:
SELECT quantity, price, owner, FROM sku_table WHERE owner=Mike
and add "" to Mike, and tested in sqlite browsers to execute the query, I do get back the row.
The working query looks like this:
SELECT quantity, price, owner, FROM sku_table WHERE owner="Mike"
Can somebody drop some insights about how do I incorporate double quotes? Other than use \"
Thanks!
Sorry, but that is exactly the reason why you should work with what the method offers! #Leandros and #Jake are helping in the totally wrong direction! Sorry to say that...
The only solution you should use is this:
Cursor findEntry = db.query("sku_table", columns, "owner=?", new String[] { owner }, null, null, null);
ps: Yes I down voted both answers as they may work but providing a solution that shouldn't be used.
Update:
If you need more than one where condition, just add it like you would do in a normal query
Cursor findEntry = db.query("sku_table", columns, "owner=? and price=?", new String[] { owner, price }, null, null, null);
The order of the ? and the new String[] {...} elements must be the same!
Cursor findEntry = db.query("sku_table", columns, "owner='"+owner+"'", null, null, null, null);
SELECT quantity, price, owner, FROM sku_table WHERE owner='Mike' this is the correct SELECT. You forget the ' ' (single quotes)
public Cursor show_vol(String vol,String bk,String hadnu)
{
SQLiteDatabase db = this.getReadableDatabase();
String[] columns ={"hadith"};//colums name that you select
Cursor res = db.query("volume2", columns, "hadithno=?", new String[] { hadnu }, null, null, null);
//volume2 is table name and hadithno is colume name l
//select hadith from volume2 where hadithno=hadnu //working like s
1. List item
return res;
}
I know this is an old question, but you can also do it like this:
Cursor findEntry = db.query("sku_table", columns, "owner=\'"+owner+"\'", null, null, null, null);
I just did it in my app and it worked as expected.
Jake's answer was similar, but probably wouldn't work without the \ before the '
the simplest way is to use SELECT col1,col2 FROM table_name WHERE col =' something' ; just like Leandros said , my problem was the single quotes , thnx
I have a question regarding how to use a WHERE clause when querying a sql database in Android. I need to return specific records from my database where the value of DURATION is greater than 3.
It works fine when I have the WHERE clause for checking equals.
Example
Cursor resultOfFilterQuery = db.query(myTable, new String[] {call_cost, call_type,
date,DURATION , phone_number }, phone_number= , new String[]{"9456788909"}, null, null, null);
Please let me know how to check for greater than
How should the query statement look?
Cursor resultOfFilterQuery = db.query(myTable, new String[] {call_cost, call_type,
date,DURATION , phone_number }, DURATION> , new String[]{3}, null, null, null);
Don't know how your first code snippet work with syntax errors, but this can helps:
Cursor resultOfFilterQuery = db.query(myTable,
new String[] {call_cost, call_type, date, DURATION, phone_number },
DURATION + "> ?", new String[]{"3"}, null, null, null);