gpt4 book ai didi

sqlite - 无法在具有外键的表中插入行

转载 作者:行者123 更新时间:2023-12-03 19:50:29 33 4
gpt4 key购买 nike

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

这是我的表:

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
enter image description here

这是我的 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;
}

最佳答案

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

关于sqlite - 无法在具有外键的表中插入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17015633/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com