java.lang.RuntimeException: Unable to start activity ComponentInfo: Sqlite exception" error

298 views Asked by At

while I am trying to execute my app, it is crashing and I am getting this "java.lang.RuntimeException: Unable to start activity ComponentInfo: Sqlite exception" error. please help me find my mistake. here is the snapshot of my code:

@Override
public void onCreate(SQLiteDatabase db) {
    this.db = db;
    final String SQL_CREATE_QUESTIONS_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_OPTION4 + " TEXT," +
            questiontable.COLUMN_ANSWER_NR + "INTEGER" +
            ")";
    db.execSQL(SQL_CREATE_QUESTIONS_TABLE);
    fillquestionstable();
}'''

here is the logcat:

Process: com.example.modaltest, PID: 6534
java.lang.RuntimeException: Unable to startactivityComponentInfo{com.example.modaltest/com.example.modaltest.quiz}:android.database.sqlite.SQLiteException: near ".": syntax error (code 1): , while compiling: CREATE TABLE quiz_questions ( _id INTEGER PRIMARY KEY AUTOINCREMENT,com.example.modaltest.questionTEXT,option1 TEXT,option2 TEXT,option3 TEXT,option4 TEXT,answer_nrINTEGER)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)at 
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
5

There are 5 answers

0
Jignesh Mayani On

You have mistake in query check this

final String SQL_CREATE_QUESTIONS_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_OPTION4 + " TEXT," +
        questiontable.COLUMN_ANSWER_NR + "INTEGER" +
        ")";
0
Rans On

You have to change this line

questiontable.COLUMN_QUESTION + "TEXT," + to questiontable.COLUMN_QUESTION + " TEXT," +

space before TEXT

0
Destroyer On
  questiontable.COLUMN_QUESTION + " TEXT," +

The problem is that in your line there is no distance between questiontable.COLUMN_QUESTION and TEXT as a result of which they merge into one line, which led to an error

1
Perraco On

There are 3 errors in the query.

The first is that you are using dots in the column name supplied in questiontable.COLUMN_QUESTION. In your example seems your are using "com.example.modaltest.question" as a column name, which can't work.

  1. Only Alphanumeric characters and underline are allowed for columns names.
  2. The column name must always begin with an alpha character or underline.

So, you need to replace "com.example.modaltest.question", by a proper column name which doesn't have any dot.

The other two errors are related to spaces. In the next declarations you need to include a space between the column name and the column type, as you have done with the other column declarations:

questiontable.COLUMN_QUESTION + "TEXT,"

and

questiontable.COLUMN_ANSWER_NR + "INTEGER"

Should be " TEXT", and " INTEGER", notice the space at the beginning of each string."

1
Pratik Butani On

There are two mistakes in your query.

1. First line questiontable.COLUMN_QUESTION + "TEXT," +

will be (give space before TEXT)

questiontable.COLUMN_QUESTION + " TEXT," +

2. Last line questiontable.COLUMN_ANSWER_NR + "INTEGER" +

will be (give space before INTEGER)

questiontable.COLUMN_ANSWER_NR + " INTEGER" +

Thank you.