如何通过使用2个字符串参数从sqlite数据库中检索特定的字符串数据? [英] how to retrieve a specific string data from sqlite database by using 2 string arguments?

查看:62
本文介绍了如何通过使用2个字符串参数从sqlite数据库中检索特定的字符串数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用于制作方法的代码

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;
} 

推荐答案

所以您可能想在选择查询中使用两个参数,这样:

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

您可以使用两种方法:

  • rawQuery()
  • query()

我将为您提供这两种情况的基本示例.

I will give you basic example for both cases.

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

说明:

因此,我建议您使用称为占位符的 ? .select语句中的每个占位符都将被selectionArgs中的值替换(顺序相同,因此第一个占位符将被数组中的第一个值替换,等等)-这是上面声明的String数组.

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.

rawQuery()方法更容易理解,所以我从它开始.Query()方法更复杂,并且具有更多的参数.所以

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

  • 列:表示将选择列数组.
  • 选择:换句话说,where子句,所以如果您的选择是 KEY_COL +"=?" 的意思是其中" + KEY_COL +"=?"
  • selectionArgs :每个占位符将替换为此值数组.
  • groupBy::它是多行(分组)功能.更多关于
  • 具有:此子句始终与group by子句一起使用这里是解释
  • orderBy:是用于基于一个或多个对行进行排序的子句列
  • 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

方法也有更多的参数,但是现在您不需要关心它们了.如果愿意,Google将成为您的朋友.

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);

因此whereClause包含两个带有占位符的参数.因此,第一个占位符将替换为"data1",第二个占位符替换为"data2".

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'

注意::我建议您查看 Android SQLite数据库和ContentProvider-教程 .

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.

这篇关于如何通过使用2个字符串参数从sqlite数据库中检索特定的字符串数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆