无法从Android上的SQLite数据库中检索数据。 (返回一个空的ArrayList)

时间:2018-04-22 20:33:50

标签: android sqlite android-sqlite sqliteopenhelper

我在尝试从SQLite数据库中提取数据时遇到了问题,我正在尝试创建一个充当问题库的Android应用程序。当我尝试从这个数据库中检索信息时,我得到一个空的问题列表,应用程序运行得很好而没有错误,但问题是为什么我的questionList为空。有人可以帮我弄清楚发生了什么。

扩展SQLiteOpenHelper的QuestionDBHelper代码

package com.cyanidesystems.unitquizapp.data;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.cyanidesystems.unitquizapp.models.Question;

import java.util.ArrayList;
import java.util.List;

import static com.cyanidesystems.unitquizapp.data.QuizQuestionsContract.QuizquestionColumns.*;



public class QuestionDBHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION= 1;
    private static final String DATABASE_NAME= "questionBankDB.db";
    public static SQLiteDatabase  questionDB;

    public QuestionDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        questionDB=db;
        //Creating a string that has the sql statements to create a table with all its columns and properties
        final String SQL_CREATE_QUESTION_BANK_TABLE_COMMAND= "CREATE TABLE " + TABLE_NAME + "(" +
                COLUMN_SUBJECT + " TEXT NOT NULL, " + COLUMN_YEAR + " TEXT NOT NULL, " + COLUMN_PAPER_NUMBER + " TEXT NOT NULL, " +
                COLUMN_QUESTION_ID + " INTEGER PRIMARY KEY,  " + COLUMN_QUESTION_STRING + " TEXT NOT NULL, " +
                COLUMN_QUEST_OPTION_A + "TEXT NOT NULL, " + COLUMN_QUEST_OPTION_B + " TEXT NOT NULL, " + COLUMN_QUEST_OPTION_C  +
                " TEXT NOT NULL, " + COLUMN_QUEST_OPTION_D + " TEXT NOT NULL, " +  COLUMN_ANSWER  + " TEXT NOT NULL )";

        db.execSQL(SQL_CREATE_QUESTION_BANK_TABLE_COMMAND);
        initQuestions();


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(" DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);

    }
    public void  initQuestions(){
//initializing the Database with some sample questions
        Question q1=new Question("Computer Science","2017","I","00","" +
                "What is the meaning of URL","Uniform resource link","Uniform resource locator","uniform risk link",
                "universal resource locator","Uniform resource locator");
        this.putThisValuesInColumn((q1));

        Question q2=new Question("Computer Science","2017","I","01","" +
                "Android was acquired by Google in","2000","2003","2005", "2007","2005");
        this.putThisValuesInColumn((q2));



    Question q3=new Question("Computer Science","2017","I","02","" +
                    "CPU stands for","central process unit","central powering unit","central processing unit","control power unit","central processing unit");        
       this.putThisValuesInColumn((q3));

        Question q4=new Question("computer science","2017","I","03","What is the meaning of BIOS","Basic input output system","Basic instruction output system","Basic in/out system","bits in/out System","Basic input output system");
        this.putThisValuesInColumn((q4));
    }

        public void putThisValuesInColumn(Question quest){
            //this method actually loads the data to be inserted for a particular column

        //questionDB=this.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(COLUMN_SUBJECT,quest.getSubject());
        values.put(COLUMN_YEAR,quest.getYear());
        values.put(COLUMN_PAPER_NUMBER,quest.getPaper_number());
        values.put(COLUMN_QUESTION_ID,quest.getQuestID());
        values.put(COLUMN_QUESTION_STRING,quest.getQuest_String());
        values.put(COLUMN_QUEST_OPTION_A,quest.getOptionA());
        values.put(COLUMN_QUEST_OPTION_B,quest.getOptionB());
        values.put(COLUMN_QUEST_OPTION_C,quest.getOptionC());
        values.put(COLUMN_QUEST_OPTION_D,quest.getOptionD());
        values.put(COLUMN_ANSWER,quest.getAnswer());



        // loads all of the initialized data into the database table
        questionDB.insert(TABLE_NAME,null,values);




    }


    public  ArrayList<Question> getAllQuestions(){
//this method extracts all the questions in the database
//and returns it as a list of question objects

            ArrayList<Question> questionList=new ArrayList<>();
            //string of selecting quest from DB
            String sqlSelectString="SELECT * FROM " + TABLE_NAME;

            questionDB=this.getWritableDatabase();
            Cursor cursor=questionDB.rawQuery(sqlSelectString,null);

            if (cursor.moveToFirst()){
                do {
                    Question question=new Question();

                    question.setSubject(cursor.getString(0));
                    question.setYear(cursor.getString(1));
                    question.setPaper_number(cursor.getString(2));
                    question.setQuestID(cursor.getString(3));
                    question.setQuest_String(cursor.getString(4));
                    question.setOptionA(cursor.getString(5));
                    question.setOptionB(cursor.getString(6));
                    question.setOptionC(cursor.getString(7));
                    question.setOptionD(cursor.getString(8));
                    question.setOptionD(cursor.getString(9));

                    //adding the question into the question list
                    questionList.add(question);

                }while (cursor.moveToNext());

            }
            return questionList;
        }

问题类的外观如何

public class Question {


    String subject;
    String year;
    String paper_number;
    String questID;
    String quest_String;
    String optionA;
    String optionB;
    String optionC;
    String optionD;
    String answer;




    //class constructor With no Parameters
    public Question( ) {
        this.subject ="";
        this.year = "";
        this.paper_number = "";
        this.questID = "";
        this.quest_String = "";
        this.optionA = "";
        this.optionB = "";
        this.optionC = "";
        this.optionD = "";
        this.answer = "";
    }

    //Class Constructor

    public Question(String subject, String year, String paper_number, String questID, String quest_String,
                    String optionA, String optionB, String optionC, String optionD, String answer) {
        this.subject = subject;
        this.year = year;
        this.paper_number = paper_number;
        this.questID = questID;
        this.quest_String = quest_String;
        this.optionA = optionA;
        this.optionB = optionB;
        this.optionC = optionC;
        this.optionD = optionD;
        this.answer = answer;
    }


    //Getters and Setters

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getPaper_number() {
        return paper_number;
    }

    public void setPaper_number(String paper_number) {
        this.paper_number = paper_number;
    }

    public String getQuestID() {
        return questID;
    }

    public void setQuestID(String questID) {
        this.questID = questID;
    }

    public String getQuest_String() {
        return quest_String;
    }

    public void setQuest_String(String quest_String) {
        this.quest_String = quest_String;
    }

    public String getOptionA() {
        return optionA;
    }

    public void setOptionA(String optionA) {
        this.optionA = optionA;
    }

    public String getOptionB() {
        return optionB;
    }

    public void setOptionB(String optionB) {
        this.optionB = optionB;
    }

    public String getOptionC() {
        return optionC;
    }

    public void setOptionC(String optionC) {
        this.optionC = optionC;
    }

    public String getOptionD() {
        return optionD;
    }

    public void setOptionD(String optionD) {
        this.optionD = optionD;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }
}

MainActivity.java文件

public class MainActivity extends AppCompatActivity {
//Declaration of Variables
    int currentPosition = 0;//this denotes the current position of the question in our List
    int numberOfCorrectAnswers = 0;
    QuestionDBHelper questionDB=new QuestionDBHelper(this);
    ArrayList<Question> quizQuestions = new ArrayList<> ();



    //@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        progressBar.setProgress(0);

        //quizQuestions = setUpQuestions(new ArrayList<Question>());
        //it creates a new instance of an arraylist and passes it to the function
        //the function then loads it with data and returns a new loaded list
        //which is then assigned to quizQuestions


        //setData();

        setData();
    }

    public void setData() {

        subjTitle.setText(getString(R.string.Subject,"Computer Science"));
        subjYr.setText(getString(R.string.Year,"2017"));
        quizQuestions =questionDB.getAllQuestions();
        Log.i("size of quizQuestions",quizQuestions.toString());
    }

使用信息过滤器记录,显示quizQuestions Arraylist为空

04-23 00:39:39.596 2536-2536/com.cyanidesystems.unitquizapp W/dalvikvm: VFY: unable to resolve virtual method 323: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList;
04-23 00:39:39.772 2536-2536/com.cyanidesystems.unitquizapp I/size of quizQuestions: []
04-23 00:39:39.836 2536-2536/com.cyanidesystems.unitquizapp W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/graphics/drawable/Icon;)

2 个答案:

答案 0 :(得分:1)

假设TABLE_NAME正确,请尝试更改代码

if (cursor.moveToFirst()){
    do {
        // create newObject
       questionList.add(question);
   } while (cursor.moveToNext());
}

只是

if (cursor.moveToFirst()){
    do {
        // create newObject
       questionList.add(question);
   } while (cursor.moveToNext());
}

答案 1 :(得分:0)

嘿伙计们,我终于找到了什么问题。我认为默认情况下,在SQLITE中创建数据库时,数据库的光标处于打开状态 当我在QuestionDBHelper类的getAllQuestions()方法中删除了 questionDB = this.getWritableDatabase(); 语句时,它工作了。

感谢所有为我的问题做出贡献的人们的社区精神