Ixa
Ixa

Reputation: 21

formatting sqlite3_exec result

Hello how would I go about formatting the result of

   sql = "SELECT * from COMPANY ORDER BY ID";

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

I read there are console commands for it on here https://www.tutorialspoint.com/sqlite/sqlite_commands.htm

so I tried

   std::string c3 = "sqlite>.header on";
   std::string c1 = "sqlite>.mode column";

   cout << system(c3.c_str()) << endl;
   cout << system(c1.c_str()) << endl;

but that didn't work to get a table format like result

How would I do this? I've been googling for 3 hours and still didn't get anywhere

Upvotes: 2

Views: 259

Answers (1)

HappyCactus
HappyCactus

Reputation: 2014

You can't control the formatting from the sqlite3 user program, so using system() to call it will not control the C API.

You should do this using the callback function. From the same tutorialpoint site:

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

Upvotes: 1

Related Questions