0

我正在寻求在 C 中打印 SQL 语句结果的帮助。我试图不将每个变量设置为指针,然后使用该指针将变量打印出来。如果我这样做了,我会有几百个变量。这是我到目前为止所尝试的。我真的迷失了如何让它正确输出。任何帮助,将不胜感激。

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

2 回答 2

0

您想要的功能是DBFetchNextDBGetCol*( DBGetColChar, DBGetColDouble, ...)。根据文档页面DBGetColChar,流程应该是这样的,每列只需要一个变量:

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);
}
于 2018-11-30T08:45:59.290 回答
0

原型 DBActivateSQL

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

它是回报int

因此 hstmt应该声明为int类型。

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

要将其打印为字符串,您需要使用%dnot %sas hsmtis of type int

sprintf(uutNum, "%d",hstmt);
                  ^^------------------------//Using %d instead of %s here
于 2018-11-29T14:00:26.423 回答