c++ - How to obtain a boolean out of a SQL WHERE condition? - Stack Overflow

admin2025-04-17  3

I'm doing an application (only for training, it's been 1.5 years since last time I've practiced) that require a login. I already done this part, the problem is that I'm not able to verify that the user inputs are the same as what's in the DB. The only thing giving me the hint that works is that the sql query show me the result, but I would like to obtain a bool out of it for a switch

#include <iostream>
#include <sqlite3.h>

using namespace std;

static int callback(void *data, int argc, char **argv, char **azColName)
{
    int i;
    fprintf(stderr, "%s: ", (void *)data);
    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[])
{
    string Username = "";
    string Password = "";

    cout << "Please, identify yourself\nUsername : ";
    cin >> Username;

    cout << "Password : ";
    cin >> Password;

    sqlite3 *db;
    char *zErrMsg = 0;
    string data = "Callback function called";
    sqlite3_stmt *res;

    int RC = sqlite3_open("SkyEyes.db", &db);
    if (RC)
    {
        cout << "\nError : ";
        cout << zErrMsg;
    }
    else
    {
        cout << "\nDatabase opened !\n";
    }

    string query = "SELECT * FROM Users WHERE USERNAME LIKE \'" + Username + "\' AND PASSWORD LIKE \'" + Password + "\';";

    sqlite3_exec(db, query.c_str(), callback, (void *)data.c_str(), &zErrMsg);

    cout << data;
}

I'm doing an application (only for training, it's been 1.5 years since last time I've practiced) that require a login. I already done this part, the problem is that I'm not able to verify that the user inputs are the same as what's in the DB. The only thing giving me the hint that works is that the sql query show me the result, but I would like to obtain a bool out of it for a switch

#include <iostream>
#include <sqlite3.h>

using namespace std;

static int callback(void *data, int argc, char **argv, char **azColName)
{
    int i;
    fprintf(stderr, "%s: ", (void *)data);
    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[])
{
    string Username = "";
    string Password = "";

    cout << "Please, identify yourself\nUsername : ";
    cin >> Username;

    cout << "Password : ";
    cin >> Password;

    sqlite3 *db;
    char *zErrMsg = 0;
    string data = "Callback function called";
    sqlite3_stmt *res;

    int RC = sqlite3_open("SkyEyes.db", &db);
    if (RC)
    {
        cout << "\nError : ";
        cout << zErrMsg;
    }
    else
    {
        cout << "\nDatabase opened !\n";
    }

    string query = "SELECT * FROM Users WHERE USERNAME LIKE \'" + Username + "\' AND PASSWORD LIKE \'" + Password + "\';";

    sqlite3_exec(db, query.c_str(), callback, (void *)data.c_str(), &zErrMsg);

    cout << data;
}
Share Improve this question edited Jan 30 at 20:01 Pepijn Kramer 13.2k3 gold badges11 silver badges25 bronze badges asked Jan 30 at 19:53 Pyrothec06Pyrothec06 11 bronze badge 4
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Bot Commented Jan 30 at 20:08
  • FYI, assigning a string variable to "" at declaration is not necessary. The std::string class' constructor already performs this. – Thomas Matthews Commented Jan 30 at 20:45
  • Don't cross the streams. Use C language I/O or C++ I/O. Mixing them is undefined behavior. – Thomas Matthews Commented Jan 30 at 20:48
  • To be honest, I would need to relearn cpp and c differences, I didn't even mixed them on purpose
转载请注明原文地址:http://anycun.com/QandA/1744893578a89120.html