1

I am new to using Android studio and have been using a guide on youtube to develop a quiz app. Everything up until now has been working until trying to bring my database of questions over. after I save the code and rerun the code on a physical device the app will open but the moment I click the start button the app shuts down. Within the logcat I get a runtime error

    java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.greencrosscodev1/com.example.greencrosscodev1.QuizSection}: 
android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY 
(code 1 SQLITE_ERROR[1]): , while compiling: CREATE TABLE quiz_question ( _idINTEGER PRIMARY KEY 
AUTOINCREMENT, questionTEXT, option1TEXT, option2TEXT, option3TEXT, answer_nrINTEGER)

It gives me a cause by saying the same thing in the code above here. The first blue hyperlink I come too takes me to my QuizDbHelper.java to line 40 displayed below. A full page of code is follwed after that. Any advice would be appriciated

package com.example.greencrosscodev1;

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

import com.example.greencrosscodev1.QuizContract.*;

import androidx.annotation.Nullable;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class QuizDbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME ="GreenCrossCodeQuiz.db";
private static final int DATABASE_VERSION = 1;

private SQLiteDatabase db;

public QuizDbHelper(@Nullable Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    this.db= db;

    final String SQL_CREATE_QUESTION_TABLE ="CREATE TABLE " +
            QuestionTable.TABLE_NAME + " ( " +
            QuestionTable._ID + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
            QuestionTable.COLUMN_QUESTION + "TEXT, " +
            QuestionTable.COLUMN_OPTION1 + "TEXT, " +
            QuestionTable.COLUMN_OPTION2 + "TEXT, " +
            QuestionTable.COLUMN_OPTION3 + "TEXT, " +
            QuestionTable.COLUMN_ANSWER_NR + "INTEGER" + ")";

    db.execSQL(SQL_CREATE_QUESTION_TABLE);
    fillQuestionTable();
}

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

private void fillQuestionTable(){
    Question Q1 = new Question("A is correct","A","B","C", 1);
    addQuestion(Q1);

    Question Q2 = new Question("B is correct","A","B","C", 2);
    addQuestion(Q2);

    Question Q3 = new Question("C is correct","A","B","C", 3);
    addQuestion(Q3);

    Question Q4 = new Question("A is correct again","A","B","C", 1);
    addQuestion(Q4);

    Question Q5 = new Question("B is correct again","A","B","C", 2);
    addQuestion(Q5);
}

private void addQuestion(Question question){
    ContentValues cv = new ContentValues();
    cv.put(QuestionTable.COLUMN_QUESTION, question.getQuestion());
    cv.put(QuestionTable.COLUMN_OPTION1, question.getOption1());
    cv.put(QuestionTable.COLUMN_OPTION2, question.getOption2());
    cv.put(QuestionTable.COLUMN_OPTION3, question.getOption3());
    cv.put(QuestionTable.COLUMN_ANSWER_NR, question.getAnswerNumber());
    db.insert(QuestionTable.TABLE_NAME, null, cv);
}

public List<Question> getAllQuestions(){
    List<Question> questionList = new ArrayList<>();
    db = getReadableDatabase();
    Cursor c = db.rawQuery("SELECT * FROM " + QuestionTable.TABLE_NAME, null);
    if(c.moveToFirst()){
        do {
            Question question = new Question();
            question.setQuestion(c.getString(c.getColumnIndex(QuestionTable.COLUMN_QUESTION)));
            question.setOption1(c.getString(c.getColumnIndex(QuestionTable.COLUMN_OPTION1)));
            question.setOption2(c.getString(c.getColumnIndex(QuestionTable.COLUMN_OPTION2)));
            question.setOption3(c.getString(c.getColumnIndex(QuestionTable.COLUMN_OPTION3)));
            question.setAnswerNumber(c.getInt(c.getColumnIndex(QuestionTable.COLUMN_ANSWER_NR)));
            questionList.add(question);
        } while (c.moveToNext());
    }
    c.close();
    return questionList;
}
}
McLovedove
  • 11
  • 2
  • Typos. You're missing several spaces in your `CREATE TABLE` statement; e.g., one before `INTEGER` in `QuestionTable._ID + "INTEGER PRIMARY KEY AUTOINCREMENT, "`, which is causing the current Exception. Make sure to correct the rest of the columns, too. – Mike M. Jun 12 '20 at 11:32
  • 1
    Mike. M Ihave done what you said and it is now runing again. Thank you for your speedy reply – McLovedove Jun 12 '20 at 11:40

0 Answers0