user1706819
user1706819

Reputation: 125

SQLite get value from DB

I am trying to make query from DB in the following way

public Cursor getManagementData() {

    String selectQuery = "SELECT  * FROM " + DBHelper.TABLE_STAFF + " WHERE " + DBHelper.COLUMN_DEPARTMENT + " GLOB " + "'management'";

    return db.rawQuery(selectQuery, null);

}

The same thing I have tried with

query(DBHelper.TABLE_STAFF, new String[] {
            DBHelper.COLUMN_ID, DBHelper.COLUMN_NAME,
            DBHelper.COLUMN_LAST_NAME, DBHelper.COLUMN_POSITION,
            DBHelper.COLUMN_DEPARTMENT, DBHelper.COLUMN_PICTURE }, null,
            new String[] { "management" }, null, null, null);

but nothing happens.

The table structure is very simple: Table staff with Column department and I want to get all entries named management.

If you will not be able to answer, please recommend where to find close solution because after googling I have not found any comprehensive examples on searching DB by entries whether with rawQuery or just query.

Thank you in advance.

I have made changes recomended by Mr.Choy it does not crashes but when open in my app list is empty. At the same time when I run the following query:

    public Cursor getEmployeesData() {

    return db.query(DBHelper.TABLE_STAFF, new String[] {
            DBHelper.COLUMN_ID, DBHelper.COLUMN_NAME,
            DBHelper.COLUMN_LAST_NAME, DBHelper.COLUMN_POSITION,
            DBHelper.COLUMN_DEPARTMENT, DBHelper.COLUMN_PICTURE }, null,
            null, null, null, null);

}

it shows all data as it should be.

Could it be the problem with cursor adapter def. I use:

        String[] from = new String[] { DBHelper.COLUMN_NAME, DBHelper.COLUMN_LAST_NAME,
            DBHelper.COLUMN_POSITION, DBHelper.COLUMN_DEPARTMENT,
            DBHelper.COLUMN_PICTURE };

    int[] to = new int[] { R.id.textView1, R.id.textView2, R.id.textView3,
            R.id.textView4, R.id.imageView1 };

Upvotes: 0

Views: 981

Answers (2)

Shayan Pourvatan
Shayan Pourvatan

Reputation: 11948

I think you forgot equal statement in your function use this:

    String selectQuery = "SELECT  * FROM " + DBHelper.TABLE_STAFF + " WHERE " + 
    DBHelper.COLUMN_DEPARTMENT + "='management'";

DBHelper.COLUMN_DEPARTMENT must be your colomn name and management must be the value in colomn that you want

you can read this Article and this for more information of basic SQL query

Upvotes: 0

Lawrence Choy
Lawrence Choy

Reputation: 6108

I think your raw query is wrong. It should be

String selectQuery = "SELECT * FROM " + DBHelper.TABLE_STAFF 
    + " WHERE " + DBHelper.COLUMN_DEPARTMENT + "='management'";

You can take a look at basic SQL query syntax here

Upvotes: 1

Related Questions