如何解决Error inserting android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: result.high (code 1299)?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (632)

每次在文件夹中插入从sqite检索到的无线电问题、值和答案时,会得到以下错误:

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);
}

约束值

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();
提问于
用户回答回答于

最简单的解决方法是执行以下操作之一:

  • 删除应用程序的数据(删除数据库)
  • 卸载应用程序(删除数据库)
  • 增加版本号(删除表并调用onCreate方法)

尝试以上方法后重新启动应用程序

扫码关注云+社区

领取腾讯云代金券