如何从sqlite中逐个获取记录?

时间:2012-04-06 11:36:45

标签: android sqlite record fetch

我有两个表“TABLE_EXAM”和“TABLE_QUESTION”。我使用以下代码获取记录 但它只显示一条记录。我需要在点击 Next 按钮后逐个显示所有记录。请给我一些参考或提示。
我不明白如何通过单击下一步按钮逐个获取记录 提前致谢。

AppearingExamActivity.java

db=new MySQLiteHelper(getBaseContext());
        db.getWritableDatabase();
        examId=db.getExamId(profile);

        final List<ObjectiveWiseQuestion> QuestionWiseProfile= db.getOneQuestion(examId);       

        for (final ObjectiveWiseQuestion cn : QuestionWiseProfile)
        {   
            db=new MySQLiteHelper(getBaseContext());
            db.getWritableDatabase();

            //db.close();
            txtQuestion.setText(cn.getQuestion());
            optionA.setText(cn.getOptionA());
            optionB.setText(cn.getOptionB());
            optionC.setText(cn.getOptionC());
            optionD.setText(cn.getOptionD());   
            correctOption=cn.getCorrectOption();

        }

    }
    btnNext.setOnClickListener(new View.OnClickListener()
    {           
        @Override
        public void onClick(View v)
        {
            try
            {
                db=new MySQLiteHelper(getBaseContext());
                db.getWritableDatabase();
                owq.getCorrectAnswer();
                owq.setExamId(examId);
                //owq.getExamId();
                owq.getQuestionId();
                db.addResultDetails(owq);
                db.close();

            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

        }
    });

MySQLiteHelper.java

public List<ObjectiveWiseQuestion> getOneQuestion(int examId)
{
   // long index = 0;
    List<ObjectiveWiseQuestion>LocwiseProfileList=new ArrayList<ObjectiveWiseQuestion>();
     db = getReadableDatabase();


     String selectQuery=("select * from question where exam_id ='"+ examId +"'");
        Cursor cursor = db.rawQuery(selectQuery, null);


    if (cursor.moveToFirst())
    {
        do {
            ObjectiveWiseQuestion owq= new ObjectiveWiseQuestion();

            owq.setQuestionId(cursor.getInt(0));
            owq.setExamId(cursor.getInt(1));
            owq.setQuestion(cursor.getString(2));
            owq.setOptionA(cursor.getString(3));
            owq.setOptionB(cursor.getString(4));
            owq.setOptionC(cursor.getString(5));
            owq.setOptionD(cursor.getString(6));
            owq.setCorrectOption(cursor.getString(7));

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

    }


    return LocwiseProfileList;
}

怎么做?

4 个答案:

答案 0 :(得分:3)

在按钮上单击只需处理cursor的位置即可。

游标inbuilt具有移动其位置的能力。 您有moveNext()movePrevious()moveToPosition()moveToFirst()moveToLast()。除此之外,它还有getCount(),它给出了记录的总数。

http://developer.android.com/reference/android/database/Cursor.html

String selectQuery=("select * from question where exam_id ='"+ examId +"'");
        Cursor cursor = db.rawQuery(selectQuery, null);

btnNext.setOnClickListener(new View.OnClickListener()
    {           
        @Override
        public void onClick(View v)
        {
            try
            {
               cursor.moveNext();
               //show on UI with cursor value
           }

        catch(Exception e)
        {
            e.printStackTrace();
        }

    }
});

答案 1 :(得分:2)

这是您必须在java文件中编写的代码。

Cursor mCursor=null;
mCursor = DatabaseObject.getDefault(); // 
if (mCursor.getCount() >= 1) {
                mCursor.moveToFirst();
                try {
                    for (int i = 0; i < mCursor.getCount(); i++) {
                        profileName.setText(mCursor.getString(0).toString()); // You can Do your work Here 
                        firstName.setText(mCursor.getString(1).toString());
                        lastName.setText(mCursor.getString(2).toString());
                        email.setText(mCursor.getString(3).toString());

                     }
            }
}

这是数据库的功能

public Cursor getDefault() throws SQLException {

    String getRT = "SELECT * from "+ PROFILE_TABLE+";";
    Cursor mCur = sqldb.rawQuery(getRT, null);
    return mCur;
}

答案 2 :(得分:1)

首先点火查询

public Cursor Value_Of_Data(String string) throws SQLException {
        // TODO Auto-generated method stub


         Cursor mCursor = db.query(Normal_Scoring,
                  new String[] {
Columns1,Columns2,Columns3,Columns4,Columns5,Columns1
},
                  Game_Date +"<='" + string + "'",
                  null,null,null,null,null);





        if (mCursor != null)
        {
        mCursor.moveToFirst();
        }
        return mCursor;
    }

现在收到像

那样的收据
  ArrayList<String> row1 = new ArrayList<String>();

          ArrayList<String> row2 = new ArrayList<String>();

          ArrayList<String> row3 = new ArrayList<String>();

          ArrayList<String> row4 = new ArrayList<String>();

          ArrayList<String> row5 = new ArrayList<String>();

          ArrayList<String> row6 = new ArrayList<String>();




    try{
            cursor1 = db.Nomal_Score_Data(dateFormat.format(dateis));
            cursor1.moveToFirst();   
            startManagingCursor(cursor1);

            for(int i=0;i<cursor1.getCount();i++){


            String rec1 = cursor1.getString(cursor1.getColumnIndex
                                ("Columns1"));



            String rec2 = cursor1.getString(cursor1.getColumnIndex
                ("Columns2"));



            String rec3 = cursor1.getString(cursor1.getColumnIndex
                ("Columns3"));



            String rec4 = cursor1.getString(cursor1.getColumnIndex
                ("Columns4"));


            String rec5 = cursor1.getString(cursor1.getColumnIndex
                    ("Columns5"));




            String rec6 = cursor1.getString(cursor1.getColumnIndex
                       ("Columns6"));


          row1.add(rec1);  

         row12.add(rec2);

          row3.add(rec3);

         row4.add(rec4);

         row5.add(rec5);

          row6.add(rec6);

}
        catch(Exception e)
        {
            Log.e("ERROR OF FETCHING ", e.toString());
            e.printStackTrace();

        }

初始化一个类似

的计数器
int count = 0 ;

按钮点击显示

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub



        switch(v.getId())
        {
        case R.id.goNext :



            // display


       text1.setText(row1.get(count));
       text2.setText(row2.get(count));
       text3.setText(row3.get(count));
       text4.setText(row4.get(count));
       text5.setText(row5.get(count));
       text6.setText(row6.get(count));

   count++;


             }

                     break; 

case R.id.goPrev :

            Toast.makeText(this, "Button Clicked goPrev",
                    Toast.LENGTH_LONG).show();



       text1.setText(row1.get(count));
       text2.setText(row2.get(count));
       text3.setText(row3.get(count));
       text4.setText(row4.get(count));
       text5.setText(row5.get(count));
       text6.setText(row6.get(count));


         count--;



                     break; 

}
}

答案 3 :(得分:1)

最后我得到了回答......我计算了记录的数量并且只减少了1.如果我有10个记录,那么我设置counter = 10并且在每次下一次点击时简单地减1。 这是一个简单的功能,帮助我。

public List<ObjectiveWiseQuestion> getOneByOneQuestion(int examId, int questionId)
{      
    List<ObjectiveWiseQuestion>LocwiseProfileList=new ArrayList<ObjectiveWiseQuestion>();
     db = getWritableDatabase(); 

     String selectQuery=("select * from question where exam_id ='" + examId + "' and question_id ='" + questionId +"'" );
        Cursor cursor = db.rawQuery(selectQuery, null);


    if (cursor.moveToFirst())
    {
        do {
            ObjectiveWiseQuestion owq= new ObjectiveWiseQuestion();

            owq.setQuestionId(cursor.getInt(0));
            owq.setExamId(cursor.getInt(1));
            owq.setQuestion(cursor.getString(2));
            owq.setOptionA(cursor.getString(3));
            owq.setOptionB(cursor.getString(4));
            owq.setOptionC(cursor.getString(5));
            owq.setOptionD(cursor.getString(6));
            owq.setCorrectOption(cursor.getString(7));

            LocwiseProfileList.add(owq);
        } while(cursor.moveToNext());
        cursor.moveToFirst();
        db.close();

    }


    return LocwiseProfileList;
}