SQCloudRowsetCols

uint32_t SQCloudRowsetCols (SQCloudResult *result);

Description

If the result of the function SQCloudResultType is RESULT_ROWSET then use this function to retrieve the number of columns in the Rowset.

Parameters

  • result: A valid SQCloudResult pointer returned by an SQCloud function.

Return value

A uint32_t representing the number of columns in the Rowset.

Example

int main (int argc, const char * argv[]) {
    // setup config
    SQCloudConfig config = {0};
    config.username = "myusername";
    config.password = "mypassword"

    SQCloudConnection *conn = SQCloudConnect("myproject.sqlite.cloud", SQCLOUD_DEFAULT_PORT, &config);
    if (SQCloudIsError(conn)) {
        printf("ERROR connecting: %s (%d)\n", SQCloudErrorMsg(conn), SQCloudErrorCode(conn));
        return -1;
    } else {
        printf("Connection to host OK...\n\n");
    }

    // choose a database first (no error check here)
    SQCloudResult *r1 = SQCloudExec(conn, "USE DATABASE mydatabase.sqlite;");
    SQCloudResultFree(r1);

    // perform a sample query
    SQCloudResult *r = SQCloudExec(conn, "SELECT * FROM mytable;");
    if (SQCloudResultType(r) == RESULT_ROWSET) {
        uint32_t nrows = SQCloudRowsetRows(r);
        uint32_t ncols = SQCloudRowsetCols(r);

        // print column names
        for (uint32_t i=0; i<ncols; ++i) {
            uint32_t len;
            char *value = SQCloudRowsetColumnName(result, i, &len);
            printf("%.*s\n", len, value);
        }

        // print values
        for (uint32_t i=0; i<nrows; ++i) {
            for (uint32_t j=0; j<ncols; ++j) {
                SQCLOUD_VALUE_TYPE type = SQCloudRowsetValueType(r, i, j);

                switch (type) {
                case VALUE_NULL: {
                    printf("NULL\n");
                    break;
                }
                case VALUE_INTEGER: {
                    int32_t value = SQCloudRowsetInt32Value(r, i, j);
                    printf("%d\n", value);
                    // int64_t value = SQCloudRowsetInt64Value(r, i, j);
                    // printf("%lld\n", value);
                    break;
                }
                case VALUE_FLOAT: {
                    float value = SQCloudRowsetFloatValue(r, i);
                    printf("%f\n", value);
                    // double value = SQCloudRowsetDoubleValue(r, i, j);
                    // printf("%f\n", value);
                    break;
                }
                case VALUE_BLOB:
                case VALUE_TEXT: {
                    uint32_t len;
                    char *value = SQCloudRowsetValue(result, i, j, &len);
                    if (type == VALUE_BLOB) printf("[%d] BLOB size %d\n", i, len);
                    else printf("[%d] %.*s\n", i, len, value);
                    break;
                }
                }
            }
        }
    }

    SQCloudResultFree(r);
}