简体   繁体   English

SQLite:在子表中,如何从两个父表中指定两个外键

[英]SQLite: in a child table, how can I specify two foreign keys from two parent tables

I am implementing SQLite using c-interface in vs2012. 我正在vs2012中使用c接口实现SQLite。

I have three tables, two of them are parents and they aren't linked together by any keys. 我有三个表,其中两个是父母,并且没有任何键将它们链接在一起。 the third one is a child, which should have two foreign-keys from the two parent tables. 第三个是孩子,应该有两个父表中的两个外键。 I tried the following but it is not working giving me the following error: 我尝试了以下操作,但无法正常工作,出现以下错误:

foreign key contraints failed

here is my implementation: first parent table: 这是我的实现:第一个父表:

CREATE TABLE Persons (
    ID   INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    Name CHAR                              NULL    ,
    Age  INT                               NULL
);

second parent table: 第二个父表:

CREATE TABLE Jobs (
    Job_ID      INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    Description CHAR                              NULL    ,
    Country     CHAR
);

the child table 子表

CREATE TABLE Persons_Jobs (
    Title   CHAR NULL,
    country CHAR NULL,
    ID      INT      ,
    Job_ID  INT      ,
    FOREIGN KEY (ID)     REFERENCES Persons(ID) ,
    FOREIGN KEY (Job_ID) REFERENCES Jobs(Job_ID)
);

note that my tables are created successfully, and the data on the first two tables are also inserted successfully. 请注意,我的表已成功创建,并且前两个表中的数据也已成功插入。


UPDATE 2: 更新2:

The insertion statements: 插入语句:

void db_prepareInsertSql(sqlite3 *db){
    sqlite3_int64 rowPersonID,rowJobID;
    int i =0;
    char *sql;
    char str[100];  

    do{
        i = i+1;
        sprintf_s(str, "INSERT INTO Persons VALUES(NULL,'liena',%d);",i);
        sql = str;
        db_execute_sql(db,sql);
        fprintf(stdout,"Persons insertion");
        rowPersonID = sqlite3_last_insert_rowid(db);

        sprintf_s(str, "INSERT INTO Jobs VALUES(NULL,'Doc','SDN');");
        sql = str;
        db_execute_sql(db,sql);
        fprintf(stdout,"Jobs insertion");
        rowJobID = sqlite3_last_insert_rowid(db);

\\the error occurs here
        sprintf_s(str, "INSERT INTO Persons_Jobs VALUES('A','krt',%d,%d);",rowPersonID,rowJobID);
        sql = str;
        db_execute_sql(db,sql);
        fprintf(stdout,"Persons_Jobs insertion");
    }
    while(i!=10);
}

The table creation: 表创建:

void db_prepareCreateTablesSql(sqlite3 *db){    
    char *sql;
    char str[500];

    sprintf_s(str, "PRAGMA foreign_keys = ON;");
    sql = str;
    db_execute_sql(db,sql); 
    fprintf(stdout,"Enable foriegn-keys feature");

    sprintf_s(str, "CREATE TABLE IF NOT EXISTS Persons("  
         "ID            INTEGER     PRIMARY KEY  AUTOINCREMENT  NOT NULL,   " 
         "Name          CHAR                                    NULL    ,   "
         "Age           INT                                     NULL    );  ");
    sql = str;
    db_execute_sql(db,sql);
    fprintf(stdout,"table Persons");

    sprintf_s(str, "CREATE TABLE IF NOT EXISTS Jobs(" 
         "Job_ID        INTEGER     PRIMARY KEY  AUTOINCREMENT  NOT NULL,   " 
         "Description   CHAR                                    NULL    ,   " 
         "Country       CHAR                                            )");         
    sql = str;
    db_execute_sql(db,sql); 
    fprintf(stdout,"table Jobs");

    sprintf_s(str, "CREATE TABLE IF NOT EXISTS Persons_Jobs("  
         "Title         CHAR                                    NULL    ,   " 
         "country       CHAR                                    NULL    ,   " 
         "ID            INT                                             ,   "
         "Job_ID        INT                                             ,   "        
         "FOREIGN KEY   (ID)        REFERENCES      Persons(ID),"        
         "FOREIGN KEY   (Job_ID)    REFERENCES      Jobs(Job_ID))       ;");
    sql = str;
    db_execute_sql(db,sql); 
    fprintf(stdout,"table Persons_Jobs");

    db_prepareInsertSql(db);
}

when I debug, i found this: 当我调试时,我发现了这一点: 在此处输入图片说明

so, my problem is in the child table, how can I specify two foreign keys from two different parent tables ? 所以,我的问题是在子表中,如何从两个不同的父表中指定两个外键?

The two inserts in the two tables ( Persons and Jobs ) may result in two different auto incremented values. 两个表( PersonsJobs )中的两个插入可能会导致两个不同的自动递增值。 So you should catch them both with (pseudo-code): 因此,您应该使用(伪代码)将它们都捕获:

INSERT INTO Persons VALUES(NULL,'liena',1);       // 1st table
rowPersonID = sqlite3_last_insert_rowid(db)       // catch PersonID

INSERT INTO Jobs VALUES(NULL,'Doc','SDN'); // 2nd table
rowJobID = sqlite3_last_insert_rowid(db)       // catch JobID

and then: 接着:

INSERT INTO Career VALUES('A','krt',%d,%d)",rowPersonID,rowJobID; 

The problem is in this line: 问题在这一行:

 sprintf_s(str, "INSERT INTO Persons_Jobs VALUES('A','krt',%d,%d);",rowPersonID,rowJobID);

The type of rowPersonID and rowJobID is sqlite3_int64 : rowPersonIDrowJobID的类型为sqlite3_int64

sqlite3_int64 rowPersonID,rowJobID;

which is 64bit type while %d expects an integer, 32bit. 这是64位类型,而%d期望使用32位整数。 You need to cast it to integer or use I64d to format 64bit number: 您需要将其转换为整数或使用I64d格式化64位数字:

sprintf_s(str, "INSERT INTO Persons_Jobs VALUES('A','krt',%I64d,%I64d);", rowPersonID,rowJobID);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一个表可以是两个父表的子表吗 - Can a single table be a child to two parent tables 父表中的两个外键引用相同的子表。 如何构建INNER JOIN? - Two foreign keys in parent table reference same child table. How to structure INNER JOIN? 如何参考父表添加两个外键并在子表中添加值 - How to add two foreign keys and add values in child table with reference of parent table 我可以有一张带两个外键的桌子吗? - Can I have a table with two foreign keys? 如何从涉及两个表的查询中获取单行。 子表的一项中重复使用2个外键 - How to get single row from a query involving two tables. 2 Foreign Keys are repeated in one entry of child table mysql表有两个来自另一个表的外键 - mysql tables have two foreign keys from another same table 如何从包含两个引用同一列的外键的SQLite表中获取信息 - How to get at the information from a SQLite table containing two foreign keys referencing the same column 我可以在 XAMPP phpmyadmin 的父表的多个子表中使用相同的外键吗? - Can I use the same foreign key in multiple child tables from the parent table in XAMPP phpmyadmin? select 如何来自具有多个外键的两个不同表 - How select from two different tables with multiple foreign keys 使用圆形外键从两个表中删除 - Delete from two tables with circular foreign keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM