将结果与用户输入C++ SqLITE3C++进行比较
我有一个属性为id,item的tableItem将结果与用户输入C++ SqLITE3C++进行比较,c++,sqlite,C++,Sqlite,我有一个属性为id,item的tableItem id|itemName 1|noodle 2|burger 基本上我想问的是,我是否可以将输入结果与数据库进行比较 记录 e、 g如果我输入面条,并且在我的数据库中有一个匹配的面条记录,它将返回find #include <stdio.h> #include <stdlib.h> #include <sqlite3.h> #include <string> #include <iost
id|itemName
1|noodle
2|burger
基本上我想问的是,我是否可以将输入结果与数据库进行比较
记录
e、 g如果我输入面条,并且在我的数据库中有一个匹配的面条记录,它将返回find
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <string>
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i<argc; i++){
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
int main(int argc, char* argv[])
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
const char *sql;
std::string itemName;
rc = sqlite3_open("test.db", &db);
if( rc ) {
// failed
fprintf(stderr, "Can't open database: %s\n",
sqlite3_errmsg(db));
}
else
{
// success
fprintf(stderr, "Open database successfully\n");
}
std::cout << "Enter a Item" << std::endl;
std::cin >> itemName;
sql = "select * from Item";
rc = sqlite3_exec(db,sql, callback, 0, &zErrMsg);
if(//how do I compare the itemName from my database against the user input)
{
}
sqlite3_close(db);
return 0;
}
您可以使用参数而不是回调来指定所需的记录:
std::cin >> itemName;
sql = "select id from Item where itemName = ?";
sqlite3_stmt *stmt;
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
// set the ? parameter to the itemname you are looking for:
sqlite3_bind_text(stmt, 1, itemName.c_str(), -1, SQLITE_TRANSIENT);
if(sqlite3_step(stmt) == SQLITE_ROW )
{
int id=sqlite3_column_int(stmt,0);
std::cout << "Found id=" << id << std::endl;
}
sqlite3_finalize(stmt);
sqlite3_close(db);