我将以下列方式将一些数据插入到我的SQLite数据库中:
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));
和
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;
}
但是我收到以下错误:
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
我做错了什么?
我的表架构:
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"
+ ");";
答案 0 :(得分:2)
你已经发表了大量的not null
陈述。这意味着您需要在那里放置一些东西,否则数据库将拒绝新条目。我注意到你的insert语句中没有COLUMN_CHECK_MD5
,这在你的模式中是必需的。看看是否还有其他人,并把东西放在那里。
答案 1 :(得分:2)
您的列有NOT NULL
个约束和18列,并且您尝试仅将值插入15列,如下所示:
Error inserting lastName= contact_id=1111 primaryContact= street= designation= compnay= city= emailId= scondaryContact= nickName=Bal Inquiry isFavorite=0 zipCode= birthDate= firstName= isBloacked=0
为每列添加值或从实际上为空的列中删除NOT NULL
约束。