这是我使用该字符串的代码
String item = item1.getText().toString();
item = item.toLowerCase();
String date = getDate();
Datahelper 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);
这是我在sqlite中的方法
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};
String whereClause = KEY_CATEGORY + " = ? and " + KEY_DATE + " = ?";
String[] whereArgs = {item,date};
Cursor c = ourDatabase.query("DATABASE_TABLE", columns, whereClause, whereArgs, null, null, null);
if(c!=null){
String price = c.getString( c.getColumnIndex(KEY_PRICE));
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};
String whereClause = KEY_CATEGORY + " = ? and " + KEY_DATE + " = ?";
String[] whereArgs = {item, date};
Cursor c = ourDatabase.query("DATABASE_TABLE", columns, whereClause, whereArgs, null, null, null);
if(c!=null){
String detail = c.getString( c.getColumnIndex(KEY_DETAILS));
return detail;
}
return null;
}
我的应用程序在使用此代码时崩溃,我不知道代码中的错误如果有人需要我可以发布整个代码请帮助我
答案 0 :(得分:0)
列索引与返回的集合没有任何共同之处:
String price = c.getString( c.getColumnIndex(KEY_PRICE));
在c
中,您只有5列(0到4)(String[] columns = new String[]{KEY_ROWID, KEY_CATEGORY,KEY_DATE,KEY_PRICE,KEY_DETAILS};
)和c.getColumnIndex(KEY_PRICE)
返回表中可能高于4的实际索引。
而不是
String price = c.getString( c.getColumnIndex(KEY_PRICE));
试
String price = c.getString(3);
答案 1 :(得分:0)
正确的代码是 -
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};
String whereClause = KEY_CATEGORY + " = ? and " + KEY_DATE + " = ?";
String[] whereArgs = {item,date};
Cursor c = ourDatabase.query("DATABASE_TABLE", columns, whereClause, whereArgs, null, null, null);
if(c!=null){
c.moveToFirst();
String price = c.getString( c.getColumnIndex(KEY_PRICE));
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};
String whereClause = KEY_CATEGORY + " = ? and " + KEY_DATE + " = ?";
String[] whereArgs = {item, date};
Cursor c = ourDatabase.query("DATABASE_TABLE", columns, whereClause, whereArgs, null, null, null);
if(c!=null){
c.moveToFirst();
String detail = c.getString( c.getColumnIndex(KEY_DETAILS));
return detail;
}
return null;
}