1

我在我的数据库表“文章”中插入一行时遇到问题,该表包含一个外键,该外键链接到表类别中的自动增量主键。

这是我的表:

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;
            }
4

1 回答 1

1

您引用的外键值是否已插入到表 B 中?如果没有,您需要在插入表 A 之前这样做。

于 2013-06-10T00:57:27.753 回答