简体   繁体   中英

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.

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:

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. 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 :

sqlite3_int64 rowPersonID,rowJobID;

which is 64bit type while %d expects an integer, 32bit. You need to cast it to integer or use I64d to format 64bit number:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM