[英]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. 两个表( Persons
和Jobs
)中的两个插入可能会导致两个不同的自动递增值。 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
: rowPersonID
和rowJobID
的类型为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.