mpwbs92
mpwbs92

Reputation: 19

Printing SQL in C

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

Upvotes: 0

Views: 482

Answers (2)

Billy Brown
Billy Brown

Reputation: 2342

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

Upvotes: 0

kiran Biradar
kiran Biradar

Reputation: 12742

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

Upvotes: 0

Related Questions