1

How can I make query using SQLiteDatabase.query ?

"Select * from table where col1 = something AND col2 IS NOT NULL"

I tried it by putting the col2 with a =? in selection String and NOT NULL in selection argument but it doesn't work.

Please tell me where m going wrong.

2

3 Answers 3

3

selectionArgs is an array of strings, and can be used only for string values.

When you use col2 = ? with the string NOT NULL, you are telling the database to check if the column's value is the eight-character string "NOT NULL".

You must write col2 IS NOT NULL directly into the selection string:

db.query("MyTable", null,
         "col1 = ? AND col2 IS NOT NULL",
         new String[] { "something" },
         null, null, null);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks CL. Appreciate you help.
0

You can use Cursor and rawQuery

Cursor c=db.rawQuery(your_query,null) 

2 Comments

even the query I have mentioned works for me. I want to know how to fire it using SQLiteDatabase.query.
There is no rawQuery with one single argument!
0
public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) 

Query the given table, returning a Cursor over the result set.

Parameters table The table name to compile the query against.

columns

A list of which columns to return. Passing null will return all columns, which is discouraged to prevent reading data from storage that isn't going to be used.

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.

selectionArgs

You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.

groupBy

A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself). Passing null will cause the rows to not be grouped.

having

A filter declare which row groups to include in the cursor, if row grouping is being used, formatted as an SQL HAVING clause (excluding the HAVING itself). Passing null will cause all row groups to be included, and is required when row grouping is not being used.

orderBy

How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.

So you can try like this

String[] args = { "first string", "[email protected]" };
Cursor cursor = db.query("TABLE_NAME", null, "name=? AND email=?", args, null,null,null);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.