2

this is my code used which i use for making method

String item = item1.getText().toString();
item = item.toLowerCase();
String date = getDate();
edited = new Datahelper(this);
edited.open();
String returnedprice = edited.getprice(item,date);
String returneddetail = edited.getdetail(item,date);
edited.close();
price.setText(returnedprice);
details.setText(returneddetail);

and this is my code of method that i am using for getting that string but here i dont know how to use the 2nd date string so that the string price that return is from a row that contains that item and that date.. please give me the code of how to do it..

public String getprice(String item ,String date) {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID,
KEY_CATEGORY,KEY_DATE,KEY_PRICE,KEY_DETAILS};
Cursor v =ourDatabase.query(DATABASE_TABLE, columns, KEY_CATEGORY + " ='" + item 
+"'",null,null, null, null);
if(v!=null){
String price = v.getString(3);
return price;
}
return null;
}
public String getdetail(String item,String date) {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, 
KEY_CATEGORY,KEY_DATE,KEY_PRICE,KEY_DETAILS};
Cursor v =ourDatabase.query(DATABASE_TABLE, columns, KEY_CATEGORY + " ='" + item +
"'",null,null, null, null);
if(v!=null){
String detail = v.getString(4);
return detail;
}
return null;
} 
2
  • you want to make select query with two arguments in where clause? Commented Mar 10, 2013 at 15:05
  • hey i am very new to android.. where is "where clause" there is only selection,selectionArgs, groupBy, having, orderBy can you please edit the whole code.. Commented Mar 10, 2013 at 15:10

2 Answers 2

4

So probably you want to use two arguments in select query so:

You can use two methods:

  • rawQuery()
  • query()

I will give you basic example for both cases.

First:

String query = "select * from Table where someColumn = ? and someDateColumn = ?";
Cursor c = db.rawQuery(query, new String[] {textValue, dateValue});

Explanation:

So i recommend to you use ? that is called placeholder. Each placeholder in select statement will be replaced(in same order so first placeholder will be replaced by first value in array etc.) by values from selectionArgs - it's String array declared above.

Second:

rawQuery() method was easier to understand so i started with its. Query() method is more complex and has a little bit more arguments. So

  • columns: represents array of columns will be selected.
  • selection: is in other words where clause so if your selection is KEY_COL + " = ?" it means "where " + KEY_COL + " = ?"
  • selectionArgs: each placeholder will be replaced with value from this array.
  • groupBy: it's multi-row (grouping) function. more about
  • having: this clause is always used with group by clause here is explanation
  • orderBy: is clause used for sorting rows based on one or multiple columns

Also method has more arguments but now you don't need to care about them. If you will, Google will be your friend.

So let's back to explanation and example:

String[] columns = {KEY_COL1, KEY_COL2};
String whereClause = KEY_CATEGORY " = ? and " + KEY_DATE + " = ?";
String[] whereArgs = {"data1", "data2"};

Cursor c = db.query("Table", columns, whereClause, whereArgs, null, null, null);

So whereClause contains two arguments with placeholder for each. So first placeholder will be replaced with "data1" and second with "data2".

When query is performed, query will look like:

SELECT col1, col2 FROM Table WHERE category = 'data1' AND date = 'data2'

Note: I recommend to you have look at Android SQLite Database and ContentProvider - Tutorial.

Also i recommend to you an usage of placeholders which provide safer and much more readable and clear solutions.

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

1 Comment

hey thanks i am going to read that everything that u have suggested and very thanks..
0

You should read any SQL tutorial to find out what a WHERE clause it and how to write it.

In Android, the selection parameter is the expression in the WHERE clause. Your query could be written like this:

c = db.query(DATABASE_TABLE, columns,
             KEY_CATEGORY + " = ? AND " + KEY_DATE + " = ?",
             new String[] { item, date },
             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.