Home > database >  printing output like the way MySQL prints
printing output like the way MySQL prints

Time:11-19

I have to print the output of a query in C the way it is displayed in MySQL. Example:

  ----------- 
 |    Name   |
  ----------- 
 |    XYZ    |
  ----------- 

Where this XYZ has been fetched by the query.

I tried using varying spaces but every went out of line every time as the size of fetched value from query varied.

CodePudding user response:

You can try like this:

    do
    {
        row = mysql_fetch_row(result_set);

        if(!row)
            break;
        else if(row_count == 0)
        {
            printf("\n\n\n\n");
            printf("%s ------------------------------------------------------------------------------------------------------------------- \n",space);
            printf("%s|                                                 PRODUCT - CAMERA                                                  |\n",space);
            printf("%s -------- ------------ ------------- ------------- ----------------- ------------ -------------- --------- --------- \n",space);
            printf("%s|  S.No  | BRAND NAME | BRAND MODEL | FORM FACTOR |    SKILL LEVEL  | LENS TYPE  |   EXPOSURE   |  PRICE  |  COUNT  |\n",space);
            printf("%s -------- ------------ ------------- ------------- ----------------- ------------ -------------- --------- --------- \n",space);
        }
        printf("%s| %-6s | %-10s | %-11s | %-11s | %-15s | %-10s | %-12s | %-7s | %-7s |\n",space,row[8],row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7]);
        row_count  ;
    }
    while(1);

You don't need to vary the spaces, just keep the size of columns in table into the field width. Example: if column length is 10, then make the field width as %-10s. If the column in an integer/float, convert it to a string before printing (mind the size).

  •  Tags:  
  • c
  • Related