【发布时间】:2020-04-12 15:28:19
【问题描述】:
我想使用 String d 作为参数从 sqlite 数据库中选择数据..
- ID ------ DATE --------- VALUE
- 1 --------11/04/2020 ----2000
- 2 --------11/04/2020 ----4000
- 3 --------11/04/2020 ----1000
- 4 --------12/04/2020 ----700
- 5 --------13/04/2020 ----300
我的getEntry() 方法如下所示:
public String getEntry(String d) {
// This value of storedEntry is returned if there is no data
String storedEntry = "Nothing!";
//here we use d to fetch data from table
String queryString = "SELECT * FROM "+TABLE_NAME+" WHERE "+DATE+"="+ d;
SQLiteDatabase db = this.getReadableDatabase();
String table;
Cursor cursor = db.rawQuery(queryString, null);
if(cursor.moveToFirst()) {
do {
storedEntry = cursor.getString(2);
} while (cursor.moveToNext());
} else {
//Do nothing!
}
return storedEntry;
}
我的onClick 方法如下所示:
showDataBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DataBaseHelper helper = new DataBaseHelper(HistoryActivity.this);
String v = helper.getEntry(dateEditText.getText().toString());
valueTextView.setText(v);
}
});
但是,它返回一个错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.darq2.myapplication, PID: 25073
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.example.darq2.myapplication.DataBaseHelper.getEntry(DataBaseHelper.java:61)
为什么??
【问题讨论】:
标签: java android database sqlite android-sqlite