Android Studio - 如何使用WHERE子句从数据库中检索数据?

时间:2016-09-15 23:20:00

标签: android sqlite

我正在尝试使用游标从我的数据库中获取1行数据。然后我尝试从光标打印特定的列。我收到错误“CursorIndexOutOfBoundsException:索引-1请求,大小为1”

Java代码:

public class edit_student_form extends AppCompatActivity {
    private dbasemanager dBase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_student_form);

        Button saveNewRecordBtn = (Button) findViewById(R.id.saveNewRecord);
        saveNewRecordBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dBase = new dbasemanager(this);
                Cursor row = dBase.retrieveOneRow("18367557");
                Toast.makeText(getBaseContext(), row.getString(0) + ", " + row.getString(1) + ", " + row.getString(2), Toast.LENGTH_LONG).show();
            }
        });
    }
}

retrieveOneRow()函数:

    public Cursor retrieveOneRow(String sId) {
    String[] columns = new String[] {"sid", "first_name", "last_name", "gender",
            "course_study", "age", "address"};
    Cursor cursor = db.query(DB_TABLE, columns, "sid=?", new String[] { sId }, null, null, null, null);
    return cursor;
}

0 个答案:

没有答案