文件首頁
MySQL 8.4 C API 開發人員指南
下載本手冊
PDF (美式信紙) - 1.4Mb
PDF (A4) - 1.4Mb


5.4.39 mysql_hex_string()

unsigned long
mysql_hex_string(char *to,
                 const char *from,
                 unsigned long length)

說明

此函數建立一個合法的 SQL 字串,用於 SQL 語句中。請參閱字串字面值

from 引數中的字串會以十六進位格式編碼,每個字元編碼為兩個十六進位數字。結果會放在 to 引數中,接著是一個終止的空位元組。

from 所指向的字串長度必須為 length 個位元組。您必須分配 to 緩衝區,使其長度至少為 length*2+1 個位元組。當mysql_hex_string() 傳回時, to 的內容會是一個以 null 終止的字串。傳回值是已編碼字串的長度,不包括終止的 null 位元組。

可以使用 X'value'0xvalue 格式將傳回值放入 SQL 語句中。但是,傳回值不包含 X'...'0x。呼叫者必須提供所需的其中一個。

範例

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));
}

範例中使用的 strmov() 函數包含在 libmysqlclient 程式庫中,其作用類似於 strcpy(),但會傳回指向第一個參數終止 null 的指標。

傳回值

放入 to 的已編碼字串的長度,不包括終止的 null 字元。

錯誤

無。