1

I keep getting this error each time i try to insert radio question, value, and answer retrieved from sqlite in asset folder.

E/SQLiteDatabase: Error inserting yans=24 question=com.example.tochukwu.myapplication.Question@ecb77c9 exp=24 hours makes a day
              android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: result.high (code 1299)
                  at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
                  at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:798)
                  at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
                  at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
                  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1502)
                  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1373)
                  at com.example.tochukwu.myapplication.ExamDBHelper.insertDataUser(ExamDBHelper.java:133)
                  at com.example.tochukwu.myapplication.Ges100Activity$2.onClick(Ges100Activity.java:184)
                  at android.view.View.performClick(View.java:6199)
                  at android.view.View$PerformClick.run(View.java:23235)
                  at android.os.Handler.handleCallback(Handler.java:836)
                  at android.os.Handler.dispatchMessage(Handler.java:103)
                  at android.os.Looper.loop(Looper.java:203)
                  at android.app.ActivityThread.main(ActivityThread.java:6251)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1073)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)

ExamDBhelper

 @Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_RESULT + "("
            + COL_0
            + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + COL_1
            + " TEXT NOT NULL,"
            + COL_2
            + " TEXT NOT NULL,"
            + COL_3
            + " TEXT NOT NULL)");

}

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

I'm using constraint values

public boolean insertDataUser(String question, String  yans, String exp){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_1,question);
    contentValues.put(COL_2,yans);
    contentValues.put(COL_3,exp);
    long result = db.insert(TABLE_RESULT,null ,contentValues);

    if(result == -1)
        return false;
    else
        return true;
}

Button click activity

 boolean isInserted = db.insertDataUser(
                    currentQ.toString(),
                    currentQ.getANSWER().toString(),
                    currentQ.getEXP().toString() );
            if(isInserted == true)
                Toast.makeText(Ges100Activity.this,"Data Inserted",Toast.LENGTH_LONG).show();
            else
                Toast.makeText(Ges100Activity.this,"Data not Inserted",Toast.LENGTH_LONG).show();

Every other thing is working except for the insert value. each time i try insert using the onClickButton i get these error on the logcat

1 Answer 1

2

The following line :- android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: result.high (code 1299)

Is indicating that the NOT NULL conflict is on the column named high in the table named result.

You insert, inserts into columns yans, question and exp, to not have the conflict you need to supply a value for the high column. Or alternately alter the structure of the table.

Perhaps the root cause is that you have changed the structure, by removing the high column from the SQL invoked in the onCreate method, not realising that the onCreate only runs once for the lifetime of the database and that any structural changes need to either force onCreate to be run, or alter the table (you'd can't directly remove a column using alter, you'd have to create a differently named table using the new structure, copy any existing data from the original to the new table drop the original table (or rename it) and then rename the new table with the original name).

If any existing data can be lost then the simplest fix is to do one of the folllowing :-

  • delete the App's data (deletes the database).
  • uninstall the App (deletes the database).
  • increase the version number (drops the table and invokes the onCreate method)

After doing one of the above rerun the App.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much..I;m grateful. it's working now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.