0

When using the SELECT operation, if there are no results the callback function do not show results in the screen. I am trying to get a way to know, previously, if there are no results. For example, using the SELECT operation I would like to create a IF that will be true when there are no results in the table. Sorry for the bad English.

The code:

#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h> 

static int callback(void *data, int argc, char **argv, char **azColName){
    int i;
    fprintf(stderr, "%s: ", (const char*)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[])
{
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
    char *sql;
    const char* data = "Callback function called";

    rc = sqlite3_open("test.db", &db);
    if( rc ){
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }else{
        fprintf(stderr, "Opened database successfully\n");
    }

    sprintf (sql, "SELECT * from TABLE WHERE ADMIN LIKE ('%s%') AND PASSWORD LIKE ('%s%');", admin, password);

    rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);

    if( rc != SQLITE_OK ){
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
    }else{
        fprintf(stdout, "Operation done successfully\n");
    }
    sqlite3_close(db);
    return 0;
}

1 Answer 1

1

If you want a finer grain control over result fetching, than do not use sqlite3_exec - use a lower level API instead, to the tune:

sqlite3_prepare(...);
do {
    sqlite3_step();
    ....
} while (something);
sqlite3_finalize(...);

That's what sqlite3_exec does anyway. sqlite3_step will allow you to detect that no rows were returned and you can do your processing within the loop directly, without the need for callback.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.