简体   繁体   中英

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". The table Programs has two columns, Id and programCode . 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 . 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. Printf prints nothing and I expected to see the result of the query so I can use this to store it into an integer. Any help would be highly appreciated.

Thanks to @CL for giving me this piece of information. 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. Thanks @CL!

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