【问题标题】:在sqlite数据库c ++中搜索
【发布时间】:2022-01-23 15:54:17
【问题描述】:

首先这是我的完整代码

/*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);

我的问题在于以下几行:

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);

我有一个 sqlite 数据库,它已正确连接到我的控制台应用程序,但是,当我在行中搜索用户输入时,不管它会运行什么 if 语句。我想检查用户输入是否可用。如果用户输入可用,则运行 if 语句,如果用户输入不可用,则运行 else 语句。 我相信“rc”有一些东西在运行。有人可以告诉我一个正确的方法吗?

【问题讨论】:

    标签: c++ sql sqlite


    【解决方案1】:

    如果 SQL 成功执行,sqlite3_exec 将返回 SQLITE_OK,无论是否返回任何行。您需要使用回调来处理查询的结果。

    有关详细信息,请参阅 the documentation of that function 以及 SQLite API 的其余部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-01
      • 2011-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-03
      相关资源
      最近更新 更多