1

After hours of unsuccessfully trying to fetch a row from my MySQL DB and saving the output to a string array, I finally decided to come here and ask for help, because I apparently can't solve it myself. This is no homework, I am merely trying to write a simple command line mysql-client that can be integrated into other C projects. As I am not extracting integers from the DB, I can't work with atoi :(

My problem is that I am not able to use sprintf() in order to write the value of row[0] into a field_array[i], because unlike I thought, row[] is not being incremented on every call to mysql_fetch row(). Actually, when calling this command, row[0] only gets updated with the content of the MYSQL_RES *struct.current_row, in this case MYSQL_RES *headers.current_row. Thus the problem is that I want to be able to call mysql_fetch_row() normally, whilst incrementing the int i for the field_array[i] to be able to successfully sprintf() all values.

Below, the relevant code. If anything is missing, please tell me :)

Used variables:

MYSQL_ROW row;
MYSQL_RES *headers;
char field_array[50][100];

Part of the code which I don't get to work as intended:

while ((row = mysql_fetch_row(headers)))
 {
     for(int i = 0; i < num_fields; i++)
     {
        printf("%s\n", row[i] ? row[i] : "NULL");
        sprintf(&field_array[i], "%s", row[i]);
     }
 }

Please help me, if possible. I am using the Connector/C MySQL API, working on a windows 7 x64 environment with mingw32/GCC (Code::Blocks).

My intention is to write all values into field_array to work with them later on.

7
  • What is the problem? You didn't say. And you couldn't find any examples in your book? Commented Nov 19, 2015 at 22:10
  • Well the problem was that I was not able to sprintf() the value of row[0] into field_array[i] because row[0] is not incremented, but replaced on every call to mysql_fetch_row(). But I didn't know that at that point so I didn't even know what the real problem was :) Commented Nov 19, 2015 at 23:05
  • Then you were not ready to post a question on SO! Please edit the problem into the question to prevent closure/deletion. Commented Nov 20, 2015 at 10:04
  • Oh well, actually I thought I described it in the first sentence, where I said "unsuccessfully trying to fetch a row from my MySQL DB and saving the output to a string array". Commented Nov 20, 2015 at 13:46
  • 1
    much much much much much better thanks Commented Nov 20, 2015 at 14:07

1 Answer 1

1

Okay, I solved it by myself. The point was to use the array-increment within the while() loop instead of the for() loop.

Now I fetch all row values and write them into my field_array, which can then be used in the rest of the function.

For everyone having the same problem and finding this topic, here is your answer!

    while ((row = mysql_fetch_row(headers)))
    {
        for(int i_row = 0; i_row < num_fields; i_row++)
        {
            if (row[i_row])
            {
                sprintf(&field_array[i_array][0], "%s", row[i_row]);
            }
            else
            {
                printf("NULL");
            }
        }
        i_array++;
    }

To demonstrate the correctly working array, I used

for (int i = 0; i < (*headers).row_count; i++)
{
    printf("%s\n", field_array[i]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

You didn't actually tell anyone what the problem was, so the chance of anyone finding this is minimal. Thanks for posting the answer, though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.