tags:

views:

461

answers:

2

When using MYSQL C API to query results. The results are returnd as a MYSQL_ROW type, which according to the MYSQL C API documentation, I can easily printf("%s", row[0] ). But what if I want to transfer the contents of row[0] into a string or a char*?

A: 

You could use sprintf(), but you will still need to know the length of the string contained in row[0] so you can allocate enough memory.

Caveat: BCS correctly points out that your data will be truncated if row[0] contains extra nul bytes. If you know how much data is stored in row[0], using memcpy() is probably a better solution.

Dana the Sane
be warned however that if you do this, it will stop at the first \0
BCS
Thanks, I've updated my answer to account for that.
Dana the Sane
+1  A: 

The %s format is only supposed to accept char* so from your description it looks like MYSQL_ROW is really a char** and taking row[0] will yield a char* anyway.

I don't see how using sprintf() (or the safer but non standard asprintf()) would be of any benefit but you may consider it if it makes you feel better.

Krunch
Any form of manipulation seems to give me a run time error.casting it as a string, strcpy, sprintf, etc etcSo has anybody found a way? It's hard to believe that an API would only allow users to retrieve results and print them out but not allow you to store it in memory.
Steve