unsigned longmysql_hex_string(char *to, const char *from, unsigned long length)This function creates a legal SQL string for use in an SQL statement. SeeString Literals.
The string in thefrom argument is encoded in hexadecimal format, with each character encoded as two hexadecimal digits. The result is placed in theto argument, followed by a terminating null byte.
The string pointed to byfrom must belength bytes long. You must allocate theto buffer to be at leastlength*2+1 bytes long. Whenmysql_hex_string() returns, the contents ofto is a null-terminated string. The return value is the length of the encoded string, not including the terminating null byte.
The return value can be placed into an SQL statement using eitherX' orvalue'0x format. However, the return value does not include thevalueX'...' or0x. The caller must supply whichever of those is desired.
char query[1000],*end;end = strmov(query,"INSERT INTO test_table values(");end = strmov(end,"X'");end += mysql_hex_string(end,"What is this",12);end = strmov(end,"',X'");end += mysql_hex_string(end,"binary data: \0\r\n",16);end = strmov(end,"')");if (mysql_real_query(&mysql,query,(unsigned int) (end - query))){ fprintf(stderr, "Failed to insert row, Error: %s\n", mysql_error(&mysql));} Thestrmov() function used in the example is included in thelibmysqlclient library and works likestrcpy() but returns a pointer to the terminating null of the first parameter.
The length of the encoded string that is placed intoto, not including the terminating null character.