简体   繁体   中英

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. 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. i believe there is something run with the "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. 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.

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