C++ sqlite3 check if a value exists in table

May 15, 2021 at 11:47am
How can I use the returned value of "sql_checkexists" function in the main?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
static int sql_checkexists (void* data, int ColCount, char** ColData, char** ColName)
{
        string val = (ColName[i], ColData[i] ? ColData[i] : "NULL");
        if (val == "1") {
            return 1;
        }
      return 0;
}

int main()
{
         
    const string quote = "\"";
    sqlite3* sqldb;
    int exit = 0;
    exit = sqlite3_open("mydb.db", &sqldb);
    string data("CALLBACK FUNCTION");
     
        
        string name = "Jack";
        string sql("SELECT CASE WHEN EXISTS (SELECT name FROM PERSON WHERE name="+ quote + name + quote +") THEN 1 ELSE 0 END;");

        int rc = sqlite3_exec(sqldb, sql.c_str(), sql_checkexists, (void*)data.c_str(), NULL);
 
        sqlite3_close(sqldb);
 
        return 0;
}
Last edited on May 15, 2021 at 11:50am
May 15, 2021 at 11:56am
if it only has 2 states, first change it to be bool type and use true and false instead of 1 and 0 unless you have a requirement to do otherwise.

you can use it in a condition, and usually would:
if(sql_checkexists(stuff....) )
do something with thing that existed
else
cout complaint about not existing


also prefer to avoid wordy code.
just say
return (ColName[i], ColData[i] ? ColData[i] : "NULL") == "1";
Last edited on May 15, 2021 at 11:57am
May 15, 2021 at 12:12pm
ColName[i] is evaluated but its value is never used as the rightmost value of a , operator is used. If ColData[i] is null, then the return value is "NULL" (char*). Also ColData[i] is of type char*. So a type char* is then compared to "1" using == ......... In the OP original, the intermediate value was converted to type std::string before the comparison so that the == was appropriate.

Perhaps (not tried):

 
return ColData[i] && strcmp(ColData[i], "1") == 0;

Last edited on May 15, 2021 at 12:17pm
May 15, 2021 at 12:16pm
Ok Thanks. This worked but I am confused on how it works?!!!


1
2
3
4
int rc = sqlite3_exec(sqldb, sql.c_str(), sql_checkexists, (void*)data.c_str(), NULL);  
if (sqlite3_exec(sqldb, sql.c_str(), sql_checkexists, (void*)data.c_str(), NULL)) {
        cout << "Error! Record Already Exists!";
    }


If you have an explanation, I want to know.
I was thinking to store the value of sql_checkexists in a variable and use it for IF statement, but how it get stored into "rc" because that's for sqlite3_exec, isn't it?
Last edited on May 15, 2021 at 12:19pm
May 15, 2021 at 12:43pm
Why are calling sqlite3_exec() twice with the same arguments?

Perhaps (not tried):

1
2
3
4
const auto exists {sqlite3_exec(sqldb, sql.c_str(), sql_checkexists, (void*)data.c_str(), NULL)};

if (exists)
    cout << "Error! Record Already Exists!\n";

May 15, 2021 at 12:55pm
@seeplus Yes, this worked and it is better!

I was using rc for SQLITE_OK check in later code and didn't change it.
Topic archived. No new replies allowed.