I’m working with ODBC to build a small SQL client capable of executing basic queries on an SQL Server. However, when I print returned results from a query, my columns and rows are unevenly spaced out.
For instance, when I execute EXEC sp_helprotect 'xp_dirtree';
, my results appear like the following:
Owner Object Grantee Grantor ProtectType Action Column
sys xp_dirtree public dbo Grant Execute
The columns are all over the place and it’s often times hard to make out which column’s data belongs to which column.
I’m currently using tabs to print each column and its data which I know is the issue.. However, is there any better way going around this and evenly printing results depending on the width of each column? How can I space each column according to it’s witdth and then print the next column in the set?
My current way of handling things is with a tab t
.
// Get total count of columns from result set
SQLUSMALLINT cCount;
SQLNumResultCols(hStmt, &cCount);
// Describe each column
SQLCHAR cName[128] = { '' };
SQLUSMALLINT cColumn;
for (cColumn = 1; cColumn <= cCount; cColumn++) {
SQLDescribeColA(hStmt, cColumn, cName, sizeof(cName), NULL, NULL, NULL, NULL, NULL);
printf("%st", cName);
}
// Fetch and print rows
SQLCHAR rResults[8192] = { '' };
SQLRETURN rRet = SQLFetch(hStmt);
while (rRet != SQL_NO_DATA && rRet != SQL_ERROR) {
printf("n"); // New line for row information
for (cColumn = 1; cColumn <= cCount; cColumn++) {
SQLGetData(hStmt, cColumn, SQL_C_CHAR, rResults, sizeof(rResults), NULL);
printf("%st", rResults);
// Empty array for next row
memset(rResults, '', sizeof(rResults));
}
rRet = SQLFetch(hStmt);
}