如何从SQLite数据库的价值? [英] how to retrieve value from the SQLite database?

查看:123
本文介绍了如何从SQLite数据库的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表是的主题。我需要通过传递结果显示的 chapter_name
Subject_id 。我的问题是,如何做到这一点?当我路过值id不返回任何东西。结果
请给一些提示。结果
这里是我的code,以供参考。

I have a two tables that are Subject and chapter. I need to display chapter_name by passing
Subject_id. My question is that how to do that? When I pass value id doesn't return anything.
Please give some hint.
Here is my code for reference.

 public List<ObjectiveWiseQuestion> getAllChapter(long subId)
    {
     List<ObjectiveWiseQuestion>LocwiseProfileList=new ArrayList<ObjectiveWiseQuestion>();
     String selectQuery=("select chapterName from chapter where subject_id ='"+ subId +"'");
      db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        if (cursor.moveToFirst())
        {
            do {
                ObjectiveWiseQuestion owq= new ObjectiveWiseQuestion();

                owq.setChapterName(cursor.getString(2));
                LocwiseProfileList.add(owq);
            } while(cursor.moveToNext());
            db.close();

        }

        return LocwiseProfileList;
    }

这也说明非法状态例外。

推荐答案

变化:

owq.setChapterName(cursor.getString(2));

这样:

/* read value from first column (chapterName) i.e. at index 0 */
owq.setChapterName(cursor.getString(0));

和不要忘记关闭数据库之前关闭光标太对了:

and dont forget to close your cursor too right before closing database:

cursor.close();
db.close();

这篇关于如何从SQLite数据库的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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