简体   繁体   中英

My application does not access/create my pre-populated database, instead it creates an empty database

I am making a quiz application. I've made a pre-populated database including questionNumber, subject, question, correctAnswer, wrongAnswer1, wrongAnswer2, and wrongAnswer3 as its columns (view the picture at the bottom). I was testing if querying a database will work, so I made a class called TestActivity with its corresponding XML file, "activity_test". I included one button and a textView. My objective is to query the database and show it inside the textView upon clicking the button. My problem is that in my DatabaseHelper class which extends with the SQLiteOpenHelper, creates an empty database instead of querying my pre-populated database. I don't know where I did wrong, but I highly suspect it's the CREATE_TABLE's fault. I might have made typos. I tried retyping and retyping the schema but still no records were shown in the textView.

In this test, I only tried to query the "wrongAnswers3" column because the database has too many records.

This is my "DatabaseAdapter" class

package com.example.quizalarm;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseAdapter {

    static class DatabaseHelper extends SQLiteOpenHelper {
        private static final String DATABASE_NAME="qasadb.db";
        private static final String TABLE_NAME="qasaMultipleChoicequizdb";
        private static final String QUESTION_NUMBER="questionNumber";
        private static final String SUBJECT="subject";
        private static final String QUESTION="question";
        private static final String CORRECT_ANSWER="correctAnswer";
        private static final String WRONG_ANSWER1="wrongAnswer1";
        private static final String WRONG_ANSWER2="wrongAnswer2";
        private static final String WRONG_ANSWER3="wrongAnswer3";
        private static final String CREATE_TABLE="CREATE TABLE "+
                TABLE_NAME+" ( "+
                QUESTION_NUMBER+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
                SUBJECT+" TEXT, "+
                QUESTION+" TEXT, "+
                CORRECT_ANSWER+" TEXT, "+
                WRONG_ANSWER1+" TEXT, " +
                WRONG_ANSWER2+" TEXT, "+
                WRONG_ANSWER3+" TEXT" +
                ")";
        private static final String DROP_TABLE="DROP TABLE IF EXISTS "+TABLE_NAME;
        private static final int DATABASE_VERSION=1;
        private Context context;

        //constructor
        public DatabaseHelper(Context context){
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            this.context = context;
            Message.message(context, "Constructor called");
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            try {
                db.execSQL(CREATE_TABLE);
                Message.message(context, "onCreate was called");
            } catch (SQLException e){
                Message.message(context, ""+e);
            }

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            try {
                Message.message(context, "onUpgrade was called");
                db.execSQL(DROP_TABLE);
                onCreate(db);
            } catch (SQLException e){
                e.printStackTrace();
            }
        }
    }

    DatabaseHelper helper;
    public DatabaseAdapter (Context context){
        helper = new DatabaseHelper(context);
    }

    public String getData(){
        SQLiteDatabase db=helper.getWritableDatabase();

        //select necessary columns from table

        String[] columns={DatabaseHelper.WRONG_ANSWER3};
        Cursor cursor=db.query(DatabaseHelper.TABLE_NAME, columns, null, null, null, null, null);
        StringBuffer sb=new StringBuffer();
        while (cursor.moveToNext()){
            String cWA3 = cursor.getString(6);
            sb.append(cWA3);
        }
        return sb.toString();
    }
}

This is my "TestActivity" class

package com.example.quizalarm;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class TestActivity extends AppCompatActivity {


    DatabaseAdapter helper;

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

        helper = new DatabaseAdapter(this);
    }

    public void viewDetails(View view){
        TextView textShowData = findViewById(R.id.textShowData);
        String data = helper.getData();
        textShowData.setText(data);
    }
}

Here's my pre-populated database: 预先填充的数据库

the schema of its "qasaMultipleChoicequizdb" table is CREATE TABLE "qasaMultipleChoicequizdb" ( "questionNumber" INTEGER PRIMARY KEY AUTOINCREMENT, "subject" TEXT, "question" TEXT, "correctAnswer" TEXT, "wrongAnswer1" TEXT, "wrongAnswer2" TEXT, "wrongAnswer3" TEXT )

My reference to this code is from this playlist . Parts/videos 160-166).

For me, It's much easier to use SQLiteAssetHelper than SQLiteOpenHelper. You don't need to use the "CREATE TABLE" schema, You just need to add a certain dependency in the build.gradle (Module: app) to use this.

Here's the link: https://www.javahelps.com/2015/04/import-and-use-external-database-in.html .

Hope it helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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