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

时间:2013-09-02 10:38:22

标签: sql database sqlite

我在vs2012中使用c-interface实现SQLite。

我有三张桌子,其中两张是父母,他们没有任何钥匙连在一起。第三个是子节点,它应该有两个父表中的两个外键。我尝试了以下但是没有给我以下错误:

foreign key contraints failed

这是我的实施: 第一个父表:

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

第二个父表:

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

子表

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)
);

请注意,我的表已成功创建,前两个表中的数据也已成功插入。


更新2:

插入声明:

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

创建表:

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

当我调试时,我发现了这个: enter image description here

所以,我的问题是在子表中,如何从两个不同的父表中指定两个外键?

2 个答案:

答案 0 :(得分:3)

两个表中的两个插入(PersonsJobs)可能会导致两个不同的自动递增值。所以你应该用(伪代码)抓住它们:

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

然后:

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

答案 1 :(得分:1)

问题出在这一行:

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

rowPersonIDrowJobID的类型为sqlite3_int64

sqlite3_int64 rowPersonID,rowJobID;

是64位类型,而%d需要一个32位整数。您需要将其强制转换为整数或使用I64d格式化64位数字:

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