android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: albums.path (code 1299)

前端 未结 2 2013
花落未央
花落未央 2021-01-01 17:34

An exception happened in my app when I add a new item into my database.

Error inserting time=2016年05月14日 23:42:03 content=zxdc
    android.database.sqlite.SQ         


        
相关标签:
2条回答
  • 2021-01-01 18:10

    You have the following NOT NULL values in your database definition:

    CONTENT
    PATH
    VIDEO
    TIME
    

    But in your addDB you only put CONTENT and TIME in ContentValues so it definitely looking for PATH and VIDEO too. If they aren't present, it will give the error. There are two solution:

    Solution 1:

    remove NOT NULL constraint from VIDEO and PATH:

            db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT," + CONTENT
                + " TEXT NOT NULL," + PATH
                + " TEXT," + VIDEO
                + " TEXT," + TIME
                + " TEXT NOT NULL)");
    

    Solution 2:

    add VIDEO and PATH to ContentValues:

    private void addDB() {
        ContentValues cv = new ContentValues();
        cv.put(SecretAlbumDB.CONTENT, editText.getText().toString());
        cv.put(SecretAlbumDB.TIME, getTime());
        cv.put(SecretAlbumDB.VIDEO, getVideo());
        cv.put(SecretAlbumDB.Path, getPath());
    
        dbWriter.insert(SecretAlbumDB.TABLE_NAME, null, cv);
    }
    
    0 讨论(0)
  • 2021-01-01 18:10

    For sure you have declared your all fields (columns) as NOT NULL and you've not put the value to those fields while inserting cv. Either make those columns null acceptable or put all the values to cv for every Columns that are NOT NULL.

    0 讨论(0)
提交回复
热议问题