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);
Related
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();
I have the following query which returns a single element (at most):
Cursor cursor = db.query(
DATABASE_TABLE_SERIES,
new String[]{KEY_PRICE},
KEY_BOOK + " = ?",
new String[]{String.valueOf(bookId)},
null,
null,
KEY_DATE+" ASC",
"1"
);
I might need to update this particular single entry in the database. Here is what I try:
ContentValues changedValues = new ContentValues();
changedValues.put(KEY_PRICE, changedPrice);
db.update(
DATABASE_TABLE_SERIES,
changedValues,
"KEY_BOOK = ? ORDERBY "+ KEYDATE +" ASC LIMIT 1",
new String[]{String.valueOf(bookId)}
);
which does not work (as I expected).
How can I update the exact entry that I query with the query result? I strongly would prefer some example code, as I probably will not understand an explanation in text-form (I am an android-beginner)!
Replace:
"KEY_BOOK = ? ORDERBY "+ KEYDATE +" ASC LIMIT 1",
with:
"KEY_BOOK = ?",
There is no ORDER BY clause or LIMIT clause on an UPDATE statement.
To update this single entry, one must create a unique identifier. A unique identifier is the id in each table. So one must first select this unique identifier, then the remainder follows easily. Here is the example code:
Cursor cursor = db.query(
DATABASE_TABLE_SERIES,
new String[]{'id'},
KEY_BOOK + " = ?",
new String[]{String.valueOf(bookId)},
null,
null,
KEY_DATE+" ASC",
"1"
);
cursor.moveToFirst();
ContentValues changedValues = new ContentValues();
changedValues.put(KEY_PRICE, changedPrice);
db.update(
DATABASE_TABLE_SERIES,
changedValues,
"id = ?",
new String[]{cursor.getString(0)}
);
Of course, one could modify the first request to query both, the price and the id in one go, if that is suitable.
I have created a Sqlite database in my android app, it looks like this:
create table msgs ( id integer primary key autoincrement, msg text not null, session text not null, sender text not null);
I can get all the entry's like this, but I don't fly understand what is happening.
String[] allColumns = {"msg","session","sender"};
Cursor cursor = database.query(msgs, allColumns, id = insertId, null, null, null, null);
What i could like to do, is to only get the latest entry with a distinct session how can i do this in android?
Edit: If this was mysql i whould do "SELECT MAX(id) AS id2,msg, session FROM msgs GROUP BY session"
But cant seam to get it to work in SQLite :/
To execute a complete SQL query, you could use rawQuery:
cursor = database.rawQuery("SELECT MAX(id) AS id2,msg, session FROM msgs GROUP BY session", null);
With query, you would have to set the parameters like this:
cursor = database.query("msgs",
new String[] { "MAX(id) AS id2", "msg", "session" },
null, null,
"session",
null, null);
Please note that using an unaggregated colum (msg) in an aggregated query does not work before SQLite version 3.7.11 (Android API version 16, Jelly Bean).
Always check documentation and be careful with types!
Your using following method:
query(
msgs, //String table: OK
allColumns, //String[] columns: OK
id = insertId, //String selection: NOT OK
// ^--- this is a boolean which will render SQL something like
// "SELECT ... WHERE TRUE ..." or "SELECT ... WHERE FALSE ..."
// causing all rows or none to be displayed
null, //String[] selectionArgs: OK
null, //String groupBy: OK
null, //String having: OK
null); //String orderBy: OK
Correction:
query(
msgs, //table
allColumns, //columns
"id = ?", //selection
new String[] {String.valueOf(insertId)}, //selectionArgs
null, //groupBy
null, //having
null); //orderBy
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.
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