简体   繁体   English

Android中SQLite数据库中的单行

[英]Single Row from SQLite Database in Android

I am having the darnest time trying to grab a row by ID from my database and can't figure out why it isn't working. 我花了最惨的时间试图从数据库中按ID抓取一行,但无法弄清楚为什么它不起作用。

In my DatabaseHelper Class I have this snippet of code 在我的DatabaseHelper类中,我有以下代码片段

public Cursor getData(String Id) {

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("select * from " + BirdDB.BIRD_TABLE + "Where BIRD_ID = ?", new String[]{Id});
        return res;

    }

Col_0 is the ID of the bird in the table. Col_0是表中小鸟的ID。 This is what I have in my activity page. 这就是我的活动页面中的内容。

public void viewASingle() {
        viewABird.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Cursor res = MainActivity.myDB.getData(txt_Id.getText().toString());
                        if (res.getCount() == 0) {

                            showMessage("Error", "Nothing Found");
                            return;
                        }
                        StringBuffer buffer = new StringBuffer();
                        while (res.moveToNext()) {

                            buffer.append("ID :" + res.getString(0) + "\n");
                            buffer.append("Name : " + res.getString(2) + "\n");
                            buffer.append("Notes : " + res.getString(3) + "\n");
                            buffer.append("Location : " + res.getString(4) + "\n");
                            buffer.append("Date And Time : " + res.getString(5) + "\n");
                        }

                        showMessage("Data", buffer.toString());
                    }
                }
            );
    }

So I made sure the rest of the code is working and it is by toast messages. 因此,我确保其余代码正常运行,并且是通过烤面包消息实现的。 The app crashes with the rawQuery statement and I believed I was doing it right. 该应用程序崩溃并显示了rawQuery语句,我认为我做对了。 I did check out other stack overflow questions and tried their statements but alas no avail. 我确实检查了其他堆栈溢出问题,并尝试了它们的语句,但是没有用。 So any idea from someone? 那么有人的想法吗?

Before the concatenation of "Where BIRD_ID = ?" 在串联"Where BIRD_ID = ?" leave a space, so the table's name doesn't stick on to the condition. 留一个空格,这样表格的名称就不会停留在条件上。 Also I suggest using exception handling, it will help you debugging and also make your programs more stable 我也建议使用异常处理,它将帮助您调试并使程序更稳定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM