1

I am trying to insert the null values when the variable really has null value. This is that method which adds the data to database,

public long  addData(String text,String date, String location,byte[] smiley,byte[] image, byte[] audio, byte[]  video)  {

        ContentValues cv = new ContentValues();

        cv.put(KEY_TEXT, text);
        cv.put(KEY_DATE, date);
        cv.put(KEY_LOCATION, location);

        if(smiley==null || image==null||  audio==null|| video==null)
        {
            if(smiley==null)
            {
                cv.putNull(KEY_SMILEY);

            }
            else
            {
                cv.put(KEY_SMILEY, smiley);

            }
            if(image==null)
            {
                cv.putNull(KEY_IMAGE);

            }
            else
            {
                cv.put(KEY_IMAGE, image);

            }
            if(audio==null)
            {
                cv.putNull(KEY_AUDIO);


            }
            else
            {
                cv.put(KEY_AUDIO, audio);


            }
            if(video==null)
            {
                cv.putNull(KEY_VIDEO);
            }
            else
            {
                cv.put(KEY_VIDEO, video);
            }
        }




        long val=ourDatabase.insertOrThrow(TABLE_NAME, null, cv);

        return val;
    }

I have created table using

@Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(" CREATE TABLE " + TABLE_NAME + "( "+ KEY_TEXT + " TEXT NOT NULL, " +
                    KEY_DATE + " TEXT NOT NULL UNIQUE, " +
                    KEY_LOCATION + " TEXT , " +KEY_SMILEY+ " BLOB, " 
                    + KEY_IMAGE + " BLOB , " + KEY_AUDIO + " BLOB, " 
                    + KEY_VIDEO + " BLOB, " + KEY_ID+" INTEGER  PRIMARY KEY "+");"
                    );
        }

but unfortunately it throws exception android.database.sqlite.SQLiteConstraintException: Diary.smiley may not be NULL (code 19)

Rahul Matte
  • 1,151
  • 2
  • 23
  • 54
  • Post your DDL, please. – 323go Mar 17 '14 at 13:45
  • apparently you have added a `not null` constraint on your column, therefore you cannot set its value to null. remove the constraint in the database create query – njzk2 Mar 17 '14 at 13:47
  • @njzk2 yeah...but it is only for column KEY_TEXT & KEY_DATE – Rahul Matte Mar 17 '14 at 13:48
  • 3
    If you had the constraints in a previous version of your code, make sure the database file is updated, too. For example, uninstall the app to remove the old database. – laalto Mar 17 '14 at 13:50
  • @laalto thanks worked like charm – Rahul Matte Mar 17 '14 at 13:53
  • possible duplicate of [When is SQLiteOpenHelper onCreate() / onUpgrade() run?](http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run) – laalto Mar 17 '14 at 13:56

1 Answers1

0

It seems like you put a constraint (NOT NULL) on the field where you create the database which prevents you from inserting Null. You might want to provide the code where you create the table.

Huxley
  • 127
  • 4