简体   繁体   English

在 sqlite 数据库 c++ 中搜索

[英]search in sqlite database c++

first of all this is my full code首先这是我的完整代码

/*Getting user input and checking it using sqlite*/
string data, sql;
int id, rc;
int al;
do {
    cout << "Enter your id:";
    cin >> id;
} while (id > 9999999999 || id < 1000000000);
/*Creating a sqlite3 pointer in order to connect to the sql*/
sqlite3* connection = nullptr;
/*opening sql*/
int result = sqlite3_open("sql.db", &connection);
/*Creating a query to search sql*/
if (result) {
    cerr << "Error open DB " << sqlite3_errmsg(connection) << std::endl;
}
else {
    cout << "Opened Database Successfully!" << std::endl;
}
sqlite3* query = nullptr;
/*Searching database for id*/
data = ("CALLBACK FUNCTION");
sql = ("SELECT * FROM Doctors WHERE nationalCode = " + to_string(id) + " ;");
/*start searching*/
/*if sql found a document, then it will continue; else, it will make a new document then 
it will continue*/
rc = sqlite3_exec(connection, sql.c_str(), callback, (void*)data.c_str(), NULL);

if (rc == SQLITE_OK) {
    cout << "Operation OK!" << endl;
    cout << "What do you want to do?" << endl;
    cout << "1:making a Visit" << endl;
    cout << "2:cancel your Visit" << endl;
    cout << "3:edit your Information" << endl;
    cout << "4:delete your Account" << endl;
    cin >> al;
    switch (al)
    {
    case 1:
        createVisit();
        break;
    case 2:
        cout << "Hello";
        break;
    case 3:
        editDoctor();
        break;
    case 4:
        cout << "Hello";
        break;
    default:
        cout << "Wrong number, Closing the Application";
        break;
    }
}
else {
    cout << "we can't find your information! Creating a new document right now" << endl
        << "+______________________________________________________________________________+" << endl;
    sqlite3_close(connection);
    createDoctor();
}
sqlite3_close(connection);

my problem is with these lines:我的问题是这些行:

data = ("CALLBACK FUNCTION");
sql = ("SELECT * FROM Doctors WHERE nationalCode = " + to_string(id) + " ;");
/*start searching*/
/*if sql found a document, then it will continue; else, it will make a new document then 
it will continue*/
rc = sqlite3_exec(connection, sql.c_str(), callback, (void*)data.c_str(), NULL);

i have a sqlite database and it's connected to my console app correctly but, when i'm searching for user input in the rows, no matter what it will run the if statement.我有一个 sqlite 数据库,它已正确连接到我的控制台应用程序,但是当我在行中搜索用户输入时,不管它会运行什么 if 语句。 i want to check if the user input is available.我想检查用户输入是否可用。 if the user input was available, run the if statement and if the user input was not available, then run the else statement.如果用户输入可用,则运行if语句,如果用户输入不可用,则运行else语句。 i believe there is something run with the "rc".我相信“rc”有一些东西在运行。 can someone show me a proper way to do this?有人可以告诉我一个正确的方法吗?

sqlite3_exec will return SQLITE_OK if the SQL was successfully executed, regardless of whether any rows were returned.如果 SQL 成功执行, sqlite3_exec将返回SQLITE_OK ,无论是否返回任何行。 You need to use the callback to process the results of the query.您需要使用回调来处理查询的结果。

For more information, see the documentation of that function , and of the rest of the SQLite API.有关详细信息,请参阅function和 rest 的文档 SQLite ZDB9474238D14CADE

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

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