0

I'm looking for help with printing the results of an SQL statement out in C. I'm trying not to set each variable to a pointer and then using that pointer to print the variable out. If I did, I'd have like a couple hundred variables. This is what I've tried so far. I'm literally lost on how to get this to output correctly. Any help would be appreciated.

int hstmt = DBActivateSQL(hdbc, "SELECT * FROM REG_RESULTSTABLE");
if (hstmt <= 0)
{
    ShowError();
}

sprintf(uutNum, "%s \n", hstmt);
while((resCode = DBFetchNext(hstmt)) == DB_SUCCESS) {
    SetCtrlVal(panelHandle, PANEL_lstregulator, uutNum);
}
Billy Brown
  • 2,272
  • 23
  • 25
mpwbs92
  • 19
  • 6

2 Answers2

0

Prototype of DBActivateSQL is

  int DBActivateSQL (int connectionHandle, char SQLStatement[]);

It is returns int.

Hence hstmt should be declared as int type.

int hstmt = DBActivateSQL(hdbc, "SELECT * FROM REG_RESULTSTABLE");

To print it to string you need use %d not %s as hsmt is of type int.

sprintf(uutNum, "%d",hstmt);
                  ^^------------------------//Using %d instead of %s here
kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
  • Okay, that gives me the integer of 1 which is correct meaning it's done its query but I need the query to print out. Which it isn't at the moment. – mpwbs92 Nov 29 '18 at 14:05
  • @mpwbs92 I did not understand. You want to print it out to display? – kiran Biradar Nov 29 '18 at 14:08
  • Yes, like the whole array or query of data I want to print it out in a display box which I set in the SetCtrlVal(). – mpwbs92 Nov 29 '18 at 14:11
  • Okay, make this simple so I don't confuse you. In SQL, you query your database by saying this SELECT * FROM REG_RESULTSTABLE. Maybe with a condition like WHERE OPERATOR = 'KIRAN'. I need what this queries which is the entire database at the moment to be printed out into the display I set with SetCtrlVal(). – mpwbs92 Nov 29 '18 at 14:18
  • @mpwbs92 You don't ever change `uutNum` within your while loop - how would you expect to be shown anything different than 1? – Aconcagua Nov 29 '18 at 14:40
  • I understand that, but I need the data from my database to be printed out with headers and all that jazz. It's about a 300 x 1000 or bigger database full of data that I can't disclose. That's what I may end goal is. Which isn't happening... – mpwbs92 Nov 29 '18 at 14:51
0

The functions that you want are DBFetchNext and DBGetCol* (DBGetColChar, DBGetColDouble, ...). According to the documentation page on DBGetColChar, the flow should be something like this, where you only need one variable per column:

void print_MyTable(int hdbc)
{
    char *var1;
    int var2;
    float var3;

    int statement = DBActivateSQL(hdbc, "SELECT col1, col2, col3 FROM MyTable");
    int resultCode;
    while ((resultCode = DBFetchNext(statement)) == DB_SUCCESS) {
        if ((resultCode = DBGetColChar(statement, 1, &var1, "")) != DB_SUCCESS) {
            // Handle the error
            break;
        }
        if ((resultCode = DBGetColInt(statement, 2, &var2)) != DB_SUCCESS) {
            // Handle the error
            DBFree(var1);
            break;
        }
        if ((resultCode = DBGetColFloat(statement, 3, &var3)) != DB_SUCCESS) {
            // Handle the error
            DBFree(var1);
            break;
        }

        // Print the values
        printf("col1: %s, col2: %d, col3: %f\n", var1, var2, var3);

        // Free the string's memory
        DBFree(var1);
    }
    statement = DBDeactivateSQL(statement);
}
Billy Brown
  • 2,272
  • 23
  • 25