0

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 }; 
2
  • why in first function you don't have equal? Commented Nov 22, 2013 at 8:02
  • it says syntax error. What about just query. What am I doing wrong. And do you know any sites where I can find comprehensive info on query use. Commented Nov 22, 2013 at 8:08

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

@user1706819 What is the value of DBHelper.COLUMN_DEPARTMENT?
@user1706819 Are you sure the value is management? It defines a table column name which should be something like department. I suspect it is causing error in the SQL, thats why I am asking it's value.
public static final String COLUMN_DEPARTMENT = "department";
sorry, I did not get your question. I thought you are asking about value to be retireved.
@user1706819 Then the query should be fine. You can take a look at the link I provided in the answer, hope that helps you solve the problem!
|
0

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

1 Comment

I saw Vogel's example and find his resource as very valuable for new developers like me. But he does'not provide any examples on quering by values

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.