Weird things being printed when printing from database

Hello,

I am currently having this issue where when I am printing items from a database it is printing (null): before every entry. For example...

1
2
3
  (null): Color = Blue
Brand = Nike
Name = Air Max


I tried switching some code around from my callback function, but I can't seem to find where the (null): is coming from.

Here is my code
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Program Name: M07 Programming.cpp //
// Name: Dominic Wenger //
// Date: 05/02/2022 //
// Purpose: Create an NBA Database //
#include <iostream>
#include "sqlite/sqlite3.h"
#include <string>

using namespace std;

// callback function //
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;
}

void menu()
{
    sqlite3* db;
    sqlite3_stmt* stmt;
    sqlite3_open("M08.db", &db);

    string data("CALLBACK FUNCTION");

    string sql;

    int choice;

    cout << "Welcome to the Shoe Casa Information Center!\n\nWhat would you like to do?\n\n1. See all avaliable items\n2. See all items of a certain brand\n3. Check sales of each item\n4. Check stock of each item\n5. Exit" << endl;

    sql = "SELECT Color,Brand,Name FROM SHOES";
    sqlite3_exec(db, sql.c_str(), callback, NULL, NULL);
}

int main()
{
    menu();


	return 0;
}
In your callback function, you see that fprintf() before the loop that actually prints the columns ?!

This prints the first parameter (i.e. void* data) of the callback function. And that parameter simply relays the fourth argument that was given to the invocation of sqlite3_exec() – which is set to NULL in your code!

From the SQLite3 docs:
The 4th argument to sqlite3_exec() is relayed through to the 1st argument of each callback invocation.
Last edited on
Thanks so much, never would of thought it was that simple :)
Topic archived. No new replies allowed.