Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add WHERE clause to Query on android

I would like to limit the results to those whose KEY_HOMEID is equal to journalId. I've been on this for a couple days any help would be appreciated.

public Cursor fetchAllNotes(String journalId) { return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_HEIGHT, KEY_BODY, KEY_HOMEID},"FROM DATABASE_TABLE WHERE KEY_HOMEID = journalId",null, null, null, null,null); }

like image 309
Brian Avatar asked Jul 17 '10 13:07

Brian


People also ask

Can we use WHERE in SELECT?

The basic syntax for the WHERE clause when used in a MySQL SELECT WHERE statement is as follows. “WHERE” is the keyword that restricts our select query result set and “condition” is the filter to be applied on the results. The filter could be a range, single value or sub query.

Do you need a WHERE clause for a query?

The SQL WHERE clause is something you must master if you wish to use SQL for working with data. It is arguably one of the most basic and must-learn constructs of SQL. In fact, in my experience, I have hardly found any data retrieval or manipulation queries that do not use a WHERE clause.

What is the function of RawQuery () in Android?

On the other hand, RawQuery serves as an escape hatch where you can build your own SQL query at runtime but still use Room to convert it into objects. RawQuery methods must return a non-void type. If you want to execute a raw query that does not return any value, use RoomDatabase#query methods.

Why do we use WHERE clause in query give example?

WHERE Clause is used to filter the records from the table or used while joining more than one table. Only those records will be extracted who are satisfying the specified condition in WHERE clause. It can be used with SELECT, UPDATE, DELETE statements.


2 Answers

Have a look at http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#query

Your query should look a little like this:

mDb.query(DATABASE_TABLE, // Table name
          columnNames, // String[] containing your column names
          KEY_HOMEID+" = "+jounalId, // your where statement, you do not include the WHERE or the FROM DATABASE_TABLE parts of the query,
          null,
          null,
          null,
          null
         );

If you feel more comfortable writing sql queries you can also use:

mDb.rawQuery("SQL STATEMENT", null);
like image 52
m6tt Avatar answered Oct 13 '22 00:10

m6tt


It makes your code more clear if you'll use it where arguments (in query parameters) Example:

        String [] settingsProjection = {
                DBContract.Settings._ID,
                DBContract.Settings.COLUMN_NAME_USER_ID,
                DBContract.Settings.COLUMN_NAME_AUTO_LOGIN
        };

        String whereClause = DBContract.Settings.COLUMN_NAME_USER_ID+"=?";
        String [] whereArgs = {userId.toString()};

        Cursor c = db.query(
                DBContract.Settings.TABLE_NAME,
                settingsProjection,
                whereClause,
                whereArgs,
                null,
                null,
                null
        );
like image 26
Krystian Avatar answered Oct 13 '22 01:10

Krystian