Unable to insert records in SQLite in android

I am inserting some data in the following way into my SQLite database :

                  Contact contactData = new Contact();
                  contactData.setContact_id("1111");//String.valueOf(id)
                 //contactData.setNickName(name);
                 // contactData.setPrimaryContact(primary);

                  HashMap<String, Contact> map =  new  HashMap<String, Contact>();
                  map.put("data", contactData);

                  long newId = datasource.createContact(map);                     
                  Log.e("insert Contact id :", String.valueOf(newId));

and

public long createContact(HashMap<String, Contact> queryValues) {
ContentValues values = new ContentValues();

Contact val = (Contact) queryValues.get("data");

 values.put(  MySQLiteHelper.COLUMN_CONTACT_ID,  val.getContact_id());
values.put(  MySQLiteHelper.COLUMN_FIRST_NAME,  val.getFirstName());
values.put(  MySQLiteHelper.COLUMN_LAST_NAME, val.getLastName());
values.put(  MySQLiteHelper.COLUMN_NICK_NAME, val.getNickName());
values.put(  MySQLiteHelper.COLUMN_BIRTHDATE, val.getBirthDate());
values.put(  MySQLiteHelper.COLUMN_PRIMARY_CONTACT, val.getPrimaryContact());
values.put(  MySQLiteHelper.COLUMN_SECONDARY_CONTACT, val.getSecondaryContact());
values.put(  MySQLiteHelper.COLUMN_EMAIL_ID, val.getEmailId());
values.put(  MySQLiteHelper.COLUMN_STREET, val.getStreet());
values.put(  MySQLiteHelper.COLUMN_CITY, val.getCity());
values.put(  MySQLiteHelper.COLUMN_ZIP_CODE, val.getZipCode());
values.put(  MySQLiteHelper.COLUMN_COMPANY, val.getCompany());
values.put(  MySQLiteHelper.COLUMN_DESIGNATION,  val.getDesignation());
values.put(  MySQLiteHelper.COLUMN_IS_FAVORITE,  val.getIsFavorite());
values.put(  MySQLiteHelper.COLUMN_IS_BLOCKED,  val.getIsBloacked());    
values.put(  MySQLiteHelper.COLUMN_IS_CREATED,  new Date().toString());    
values.put(  MySQLiteHelper.COLUMN_IS_BLOCKED,  AESCryptography.getMd5(val.getFirstName()+" "+val.getLastName()+":"+val.getPrimaryContact()+":"+val.getEmailId()));   

long insertId = database.insert(MySQLiteHelper.CONTACT_TABLE_NAME, null, values);
return insertId;
}

but i am getting the following error :

 02-06 17:13:18.539: E/SQLiteDatabase(8112): Error inserting lastName= contact_id=1111 primaryContact= street= designation= compnay= city= isCreated=Thu Feb 06 17:13:18 IST 2014 emailId= scondaryContact= nickName= isFavorite=0 zipCode= birthDate= firstName= isBloacked=�d�t�fVv�� ��
02-06 17:13:18.539: E/SQLiteDatabase(8112): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
02-06 17:13:18.539: E/SQLiteDatabase(8112):     at android.database.sqlite.SQLiteStatement.native_executeInsert(Native Method)
02-06 17:13:18.539: E/SQLiteDatabase(8112):     at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:113)
02-06 17:13:18.539: E/SQLiteDatabase(8112):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1718)
02-06 17:13:18.539: E/SQLiteDatabase(8112):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1591)
02-06 17:13:18.539: E/SQLiteDatabase(8112):     at com.ecosmob.contactpro.model.ContactDataSource.createContact(ContactDataSource.java:72)
02-06 17:13:18.539: E/SQLiteDatabase(8112):     at com.ecosmob.contactpro.service.ContactManagerService.onHandleIntent(ContactManagerService.java:88)
02-06 17:13:18.539: E/SQLiteDatabase(8112):     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
02-06 17:13:18.539: E/SQLiteDatabase(8112):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 17:13:18.539: E/SQLiteDatabase(8112):     at android.os.Looper.loop(Looper.java:137)
02-06 17:13:18.539: E/SQLiteDatabase(8112):     at android.os.HandlerThread.run(HandlerThread.java:60)
02-06 17:13:18.539: E/insert Contact id :(8112): -1

What am I doing wrong?

My table schema:

  private static final String CONTACT_TABLE = "create table "
  + CONTACT_TABLE_NAME + "(" 
  + COLUMN_ID                   + " integer primary key autoincrement, " 
  + COLUMN_CONTACT_ID           + " text not null, " 
  + COLUMN_FIRST_NAME           + " text not null,"
  + COLUMN_LAST_NAME            + " text not null,"
  + COLUMN_NICK_NAME            + " text not null,"
  + COLUMN_BIRTHDATE            + " text not null,"
  + COLUMN_PRIMARY_CONTACT      + " text not null,"
  + COLUMN_SECONDARY_CONTACT    + " text not null,"
  + COLUMN_EMAIL_ID             + " text not null,"
  + COLUMN_STREET               + " text not null,"
  + COLUMN_CITY                 + " text not null,"
  + COLUMN_ZIP_CODE             + " text not null,"
  + COLUMN_COMPANY              + " text not null,"
  + COLUMN_DESIGNATION          + " text not null,"
  + COLUMN_IS_FAVORITE          + " integer not null,"
  + COLUMN_IS_BLOCKED           + " integer not null,"
  + COLUMN_IS_CREATED           + " TIMESTAMP DEFAULT CURRENT_TIMESTAMP,"
  + COLUMN_CHECK_MD5            + " text not null"
  + ");";

Answers


You've put in a whole bunch of not null statements. These mean you are required to put something there, or else the database will reject a new entry. I noticed you don't have a COLUMN_CHECK_MD5 in your insert statement, and that is required in your schema. Look to see if there are others, and put something in there.


Your columns have NOT NULL constraints and 18 columns and you're trying to insert a value only to 15 columns as shown here:

Error inserting lastName= contact_id=1111 primaryContact= street= designation= compnay= city= emailId= scondaryContact= nickName=Bal Inquiry isFavorite=0 zipCode= birthDate= firstName= isBloacked=0

Either add a value to each column or remove the NOT NULL constraints from columns that can in fact be empty.


Need Your Help

Displaying a .txt file as pure text without editing the file contents

html text pre php-include

I have a .txt file containing code that I cannot change the contents of. And I need to display it in two ways.