简体   繁体   English

具有C ++的SQLite3检索ID并存储到另一个表

[英]SQLite3 with C++ retrieve ID and store to another table

Good evening people, I am building a database using SQLite3 and C++ and I want to insert into table "Subs" as a foreign key, the ID which is the primary key for table "Programs". 晚上好,我正在使用SQLite3和C ++构建数据库,我想将“ Subs”表作为外键插入,该ID是表“ Programs”的主键。 The table Programs has two columns, Id and programCode . 程序表有两列, IdprogramCode I have the programCode in a variable so my approach is to get the Id from the programCode , store it into a variable and then store this in the table Subs . 我将programCode放在变量中,所以我的方法是从programCode中获取ID ,将其存储到变量中,然后将其存储在Subs表中。 My problem is that I don't know how to get the result from query and store it into a variable. 我的问题是我不知道如何从查询中获取结果并将其存储到变量中。 This is my code: 这是我的代码:

sqlite3_stmt *sql;
rc = sqlite3_prepare_v2(db, "SELECT Id FROM Programs WHERE  programCode=?", -1, &sql, 0);
sqlite3_bind_text(sql, 1, _program.c_str(), -1,  SQLITE_TRANSIENT);
int step = sqlite3_step(sql);

if(step == SQLITE_ROW) {
   printf("%d\n", sqlite3_column_int(sql, 0));
}

The _program variable holds the programCode value. _program变量保存programCode值。 Printf prints nothing and I expected to see the result of the query so I can use this to store it into an integer. Printf不打印任何内容,我希望看到查询结果,因此可以使用它将其存储为整数。 Any help would be highly appreciated. 任何帮助将不胜感激。

Thanks to @CL for giving me this piece of information. 感谢@CL给我这条信息。 I checked my csv where I retrieve the information for the database and the programCode had a "space" before the text so that is why there was no matching row. 我检查了我的csv,在其中检索了数据库的信息,并且programCode在文本之前有一个“空格”,因此这就是为什么没有匹配的行的原因。 Thanks @CL! 谢谢@CL!

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

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