我在我的数据库表“文章”中插入一行时遇到问题,该表包含一个外键,该外键链接到表类别中的自动增量主键。
这是我的表:
static String CREATE_CATEGORIE = "create table "
+ TABLE_CATEGORIE + "("
+ COLUMN_ID_CATEGORIE + " Integer primary key autoincrement, "
+ COLUMN_NOMCAT + " text not null, "
+ COLUMN_CHILDS + " text, "
+ COLUMN_ROOT + " boolean not null, "
+ COLUMN_CAT_STRUCT + " text not null);";
static String CREATE_UPLOAD_2 = "create table "
+ TABLE_UPLOAD + "(" + COLUMN_ID
+ " Integer primary key autoincrement, "
+ COLUMN_NAME + " text not null, "
+ COLUMN_DESCRIPTION + " text not null, "
+ COLUMN_PRIX + " text not null, "
+ COLUMN_STATUS + " integer not null, "
+ COLUMN_SPECEFIC + " text not null, "
+ COLUMN_ID_CATEGORIE + "Integer not null, "
+"FOREIGN KEY ("+COLUMN_ID_CATEGORIE+") REFERENCES "+TABLE_CATEGORIE+" ("+COLUMN_ID_CATEGORIE+"));";
一旦执行了这行代码:
long idArticle= database.uploadProduct(Name, Description, Prix, specefic, idCategorie, 2);
//idCategorie = 9
在 TABLE_UPLOAD 中插入一行时,我不断收到此错误:
06-10 00:41:13.922: E/Database(7332): Error inserting _id=9 prix=Hossam
specific=champ1|hhh;champ2|hhh;champ3|hhh; status=2 description=Hossam nom=Hossam
06-10 00:41:13.922: E/Database(7332): android.database.sqlite.SQLiteConstraintException:
error code 19: constraint failed
我的表类别中已经存在值 9
这是我的 uploadProduct 方法:
public long uploadProduct(String Name,String Description,String Prix,Map<String,String> speceficInfo,int idCat,int status)
{
ContentValues values = new ContentValues();
values.put(MySqlHelper.COLUMN_NAME, Name);
values.put(MySqlHelper.COLUMN_DESCRIPTION, Description);
values.put(MySqlHelper.COLUMN_PRIX, Prix);
values.put(MySqlHelper.COLUMN_STATUS, status);
values.put(MySqlHelper.COLUMN_SPECEFIC,mapToString(speceficInfo));
values.put(MySqlHelper.COLUMN_ID_CATEGORIE, idCat);
long id=database.insert(MySqlHelper.TABLE_UPLOAD, null,values);
return id;
}