📄 月光软件站 - 编程文档 - 其他语言 - 详细的mysql c api [转].htm
字号:
<P><B style='color:black;background-color:#ffff66'>mysql</B>_query(&<B style='color:black;background-color:#ffff66'>mysql</B>,"SELECT * FROM some_table");<BR>result = <B style='color:black;background-color:#ffff66'>mysql</B>_use_result(&<B style='color:black;background-color:#ffff66'>mysql</B>);<BR>while((row = <B style='color:black;background-color:#ffff66'>mysql</B>_fetch_row(result)))<BR>{<BR> // do something with data<BR>}<BR>if(<B style='color:black;background-color:#ffff66'>mysql</B>_errno(&<B style='color:black;background-color:#ffff66'>mysql</B>)) // <B style='color:black;background-color:#ffff66'>mysql</B>_fetch_row() failed due to an error<BR>{<BR> fprintf(stderr, "Error: %s\n", <B style='color:black;background-color:#ffff66'>mysql</B>_error(&<B style='color:black;background-color:#ffff66'>mysql</B>));<BR>}<BR><BR>20.4.11 <B style='color:black;background-color:#ffff66'>mysql</B>_errno()<BR>unsigned int <B style='color:black;background-color:#ffff66'>mysql</B>_errno(<B style='color:black;background-color:#ffff66'>MYSQL</B> *<B style='color:black;background-color:#ffff66'>mysql</B>)
<P>20.4.11.1 说明<BR>对于由<B style='color:black;background-color:#ffff66'>mysql</B>指定的连接,<B style='color:black;background-color:#ffff66'>mysql</B>_errno()返回最近调用的可能成功或失败的<B style='color:black;background-color:#99ff99'>API</B>函数的错误代码。返回值零意味着没有错误发生。客户错误消息编号列出在<B style='color:black;background-color:#ffff66'>MySQL</B>“errmsg.h”头文件中。服务器错误消息编号列出在“mysqld_error.h”中。
<P>20.4.11.2 返回值:<BR>一个错误代码值。如果没有错误发生,零。
<P>20.4.11.3 错误<BR>无。
<P><BR>20.4.12 <B style='color:black;background-color:#ffff66'>mysql</B>_error()<BR>char *<B style='color:black;background-color:#ffff66'>mysql</B>_error(<B style='color:black;background-color:#ffff66'>MYSQL</B> *<B style='color:black;background-color:#ffff66'>mysql</B>)
<P>20.4.12.1 说明<BR>对于由<B style='color:black;background-color:#ffff66'>mysql</B>指定的连接,<B style='color:black;background-color:#ffff66'>mysql</B>_errno()返回最近调用的可能成功或失败的<B style='color:black;background-color:#99ff99'>API</B>函数的错误代码。如果没有错误发生,返回空字符串("")。这意味着下列两个测试是等价的:
<P>if(<B style='color:black;background-color:#ffff66'>mysql</B>_errno(&<B style='color:black;background-color:#ffff66'>mysql</B>))<BR>{<BR> // an error occurred<BR>}
<P>if(<B style='color:black;background-color:#ffff66'>mysql</B>_error(&<B style='color:black;background-color:#ffff66'>mysql</B>)[0] != '\0')<BR>{<BR> // an error occurred<BR>}
<P>客户错误消息的语言可通过重新编译<B style='color:black;background-color:#ffff66'>MySQL</B>客户库来改变。目前,你能在几种不同的语言间选取错误消息。见9.1 <B style='color:black;background-color:#ffff66'>MySQL</B>支持什么语言?。
<P>20.4.12.2 返回值<BR>一个描述错误的字符串。如果没有错误发生,空字符串。
<P>20.4.12.3 错误<BR>无。
<P>
<P>20.4.13 <B style='color:black;background-color:#ffff66'>mysql</B>_escape_string()<BR>unsigned int <B style='color:black;background-color:#ffff66'>mysql</B>_escape_string(char *to, const char *from, unsigned int length)
<P>20.4.13.1 说明<BR>把在from中的字符串编码为在一条SQL语句中可以发给服务器的转义的SQL字符串,将结果放在to中, 并且加上一个终止的空字节。编码的字符是NUL(ASCII 0)、‘\n’、‘\r’、‘\’、‘'’、‘"’和Control-Z(见7.1 文字:如何写字符串和数字)。
<P>由from指向的字符串必须是length个字节长。你必须分配to的缓冲区至少length*2+1个字节长。(在更坏的情况,每个字符可能需要使用2个字节被编码,并且你需要为终止空字节的空间) 当<B style='color:black;background-color:#ffff66'>mysql</B>_escape_string()返回时,to的内容将是空字符终止的字符串。返回值是编码后的字符串的长度,不包括终止空字符。
<P>20.4.13.2 范例<BR>char query[1000],*end;
<P>end = strmov(query,"INSERT INTO test_table values(");<BR>*end++ = '\'';<BR>end += <B style='color:black;background-color:#ffff66'>mysql</B>_escape_string(end,"What's this",11);<BR>*end++ = '\'';<BR>*end++ = ',';<BR>*end++ = '\'';<BR>end += <B style='color:black;background-color:#ffff66'>mysql</B>_escape_string(end,"binary data: \0\r\n",16);<BR>*end++ = '\'';<BR>*end++ = ')';
<P>if (<B style='color:black;background-color:#ffff66'>mysql</B>_real_query(&<B style='color:black;background-color:#ffff66'>mysql</B>,query,(unsigned int) (end - query)))<BR>{<BR> fprintf(stderr, "Failed to insert row, Error: %s\n",<BR> <B style='color:black;background-color:#ffff66'>mysql</B>_error(&<B style='color:black;background-color:#ffff66'>mysql</B>));<BR>}
<P>例子中所用的strmov()函数被包括在mysqlclient库中且功能类似于strcpy(),但是返回一个指向空终止的第一个参数的指针。
<P>20.4.13.3 返回值<BR>放进to的值的长度,不包括终止空字符。
<P>20.4.13.4 错误<BR>无。
<P>
<P>20.4.14 <B style='color:black;background-color:#ffff66'>mysql</B>_fetch_field()<BR><B style='color:black;background-color:#ffff66'>MYSQL</B>_FIELD *<B style='color:black;background-color:#ffff66'>mysql</B>_fetch_field(<B style='color:black;background-color:#ffff66'>MYSQL</B>_RES *result)
<P>20.4.14.1 说明<BR>返回作为一个<B style='color:black;background-color:#ffff66'>MYSQL</B>_FIELD结构的一个结果集合的一个列的定义。重复调用这个函数在结果集合中检索所有关于列的信息。当没有剩下更多的字段时,<B style='color:black;background-color:#ffff66'>mysql</B>_fetch_field()返回NULL。
<P>在每次你执行一个新的SELECT查询,<B style='color:black;background-color:#ffff66'>mysql</B>_fetch_field()被重置(reset)以返回有关第一列的信息。由<B style='color:black;background-color:#ffff66'>mysql</B>_fetch_field()返回的字段也受调用<B style='color:black;background-color:#ffff66'>mysql</B>_field_seek()的影响。
<P>如果你调用<B style='color:black;background-color:#ffff66'>mysql</B>_query()在一张表上执行一个SELECT,但是没调用<B style='color:black;background-color:#ffff66'>mysql</B>_store_result(),如果你调用<B style='color:black;background-color:#ffff66'>mysql</B>_fetch_field()询问一个BLOB字段的长度,<B style='color:black;background-color:#ffff66'>MySQL</B>返回缺省BLOB长度(8K字节)。(选择8K的长度是因为<B style='color:black;background-color:#ffff66'>MySQL</B>不知道BLOB的最大长度。这应该在某个时候是它可配置) 一旦你已经检索了结果集合,field->max_length包含了在特定查询中对于该列最大值的长度。
<P>20.4.14.2 返回值<BR>当前列的<B style='color:black;background-color:#ffff66'>MYSQL</B>_FIELD结构。如果没有列剩下,NULL。
<P>20.4.14.3 错误<BR>无。
<P>20.4.14.4 范例<BR><B style='color:black;background-color:#ffff66'>MYSQL</B>_FIELD *field;
<P>while((field = <B style='color:black;background-color:#ffff66'>mysql</B>_fetch_field(result)))<BR>{<BR> printf("field name %s\n", field->name);<BR>}<BR><BR>20.4.15 <B style='color:black;background-color:#ffff66'>mysql</B>_fetch_fields()<BR><B style='color:black;background-color:#ffff66'>MYSQL</B>_FIELD *<B style='color:black;background-color:#ffff66'>mysql</B>_fetch_fields(<B style='color:black;background-color:#ffff66'>MYSQL</B>_RES *result)
<P>20.4.15.1 说明<BR>返回一个结果集合的所有<B style='color:black;background-color:#ffff66'>MYSQL</B>_FIELD结构的数组。每个结构提供结果结合中一列的字段定义。
<P>20.4.15.2 返回值<BR>一个结果集合的所有<B style='color:black;background-color:#ffff66'>MYSQL</B>_FIELD结构的一个数组。
<P>20.4.15.3 错误<BR>无。
<P>20.4.15.4 范例<BR>unsigned int num_fields;<BR>unsigned int i;<BR><B style='color:black;background-color:#ffff66'>MYSQL</B>_FIELD *fields;
<P>num_fields = <B style='color:black;background-color:#ffff66'>mysql</B>_num_fields(result);<BR>fields = <B style='color:black;background-color:#ffff66'>mysql</B>_fetch_fields(result);<BR>for(i = 0; i < num_fields; i++)<BR>{<BR> printf("Field %u is %s\n", i, fields[i].name);<BR>}
<P>20.4.16 <B style='color:black;background-color:#ffff66'>mysql</B>_fetch_field_direct()<BR><B style='color:black;background-color:#ffff66'>MYSQL</B>_FIELD *<B style='color:black;background-color:#ffff66'>mysql</B>_fetch_field_direct(<B style='color:black;background-color:#ffff66'>MYSQL</B>_RES *result, unsigned int fieldnr)
<P>20.4.16.1 说明<BR>给定在一个结果集合中的一个列的字段编号fieldnr,返回作为<B style='color:black;background-color:#ffff66'>MYSQL</B>_FIELD结构的列的字段定义。你可以使用这个函数检索任意列的义。fieldnr的值应该在从0到<B style='color:black;background-color:#ffff66'>mysql</B>_num_fields(result)-1范围内。
<P>20.4.16.2 返回值<BR>指定列的<B style='color:black;background-color:#ffff66'>MYSQL</B>_FIELD结构。
<P>20.4.16.3 错误<BR>无。
<P>20.4.16.4 范例<BR>unsigned int num_fields;<BR>unsigned int i;<BR><B style='color:black;background-color:#ffff66'>MYSQL</B>_FIELD *field;
<P>num_fields = <B style='color:black;background-color:#ffff66'>mysql</B>_num_fields(result);<BR>for(i = 0; i < num_fields; i++)<BR>{<BR> field = <B style='color:black;background-color:#ffff66'>mysql</B>_fetch_field_direct(result, i);<BR> printf("Field %u is %s\n", i, field->name);<BR>}<BR><BR>20.4.17 <B style='color:black;background-color:#ffff66'>mysql</B>_fetch_lengths()<BR>unsigned long *<B style='color:black;background-color:#ffff66'>mysql</B>_fetch_lengths(<B style='color:black;background-color:#ffff66'>MYSQL</B>_RES *result)
<P>20.4.17.1 说明<BR>返回在结果集合内的当前行的列长度。如果你计划拷贝字段值,这个长度信息对优化也是有用的,因为你可以避免调用strlen()。另外,如果结果集合中包含二进制数据,你必须使用这个函数确定数据的大小,因为strlen()对包含空字符的任何字段返回不正确的结果。
<P>空列和包含NULL的列的长度值是零。为了看清如何区分这两种情况,见<B style='color:black;background-color:#ffff66'>mysql</B>_fetch_row()的说明。
<P>20.4.17.2 返回值<BR>表示每列大小的无符号长整数的一个数组(不包括任何终止空字符)。如果出现一个错误,NULL。
<P>20.4.17.3 错误<BR><B style='color:black;background-color:#ffff66'>mysql</B>_fetch_lengths()只对结果集合的当前行有效。如果你在调用<B style='color:black;background-color:#ffff66'>mysql</B>_fetch_row()之前或在检索出在结果中的所有以后,它返回NULL。
<P>20.4.17.4 范例<BR><B style='color:black;background-color:#ffff66'>MYSQL</B>_ROW row;<BR>unsigned long *lengths;<BR>unsigned int num_fields;<BR>unsigned int i;
<P>row = <B style='color:black;background-color:#ffff66'>mysql</B>_fetch_row(result);<BR>if (row)<BR>{<BR> num_fields = <B style='color:black;background-color:#ffff66'>mysql</B>_num_fields(result);<BR> lengths = <B style='color:black;background-color:#ffff66'>mysql</B>_fetch_lengths(result);<BR> for(i = 0; i < num_fields; i++)<BR> {<BR> printf("Column %u is %lu bytes in length.\n", i, lengths[i]);<BR> }<BR>}<BR><BR>20.4.18 <B style='color:black;background-color:#ffff66'>mysql</B>_fetch_row()<BR><B style='color:black;background-color:#ffff66'>MYSQL</B>_ROW <B style='color:black;background-color:#ffff66'>mysql</B>_fetch_row(<B style='color:black;background-color:#ffff66'>MYSQL</B>_RES *result)
<P>20.4.18.1 说明<BR>检索一个结果集合的下一行。当在<B style='color:black;background-color:#ffff66'>mysql</B>_store_result()之后使用时,如果没有更多的行可见所时,<B style='color:black;background-color:#ffff66'>mysql</B>_fetch_row()返回NULL。当在<B style='color:black;background-color:#ffff66'>mysql</B>_use_result()之后使用时,当没有更多的行可检索时或如果出现一个错误,<B style='color:black;background-color:#ffff66'>mysql</B>_fetch_row()返回NULL。
<P>在行中值的数量由<B style='color:black;background-color:#ffff66'>mysql</B>_num_fields(result)给出。如果row保存了从一个对用<B style='color:black;background-color:#ffff66'>mysql</B>_fetch_row()调用返回的值,指向该值的指针作为row[0]到row[<B style='color:black;background-color:#ffff66'>mysql</B>_num_fields(result)-1]来存取。在行中的NULL值由NULL指针指出。
<P>在行中字段值的长度可以通过调用<B style='color:black;background-color:#ffff66'>mysql</B>_fetch_lengths()获得。空字段和包含NULL的字段长度都是 0;你可以通过检查该值的指针区分他们。如果指针是NULL,字段是NULL;否则字段是空的。
<P>20.4.18.2 返回值<BR>下一行的一个<B style='color:black;background-color:#ffff66'>MYSQL</B>_ROW结构。如果没有更多的行可检索或如果出现一个错误,NULL。
<P>20.4.18.3 错误<BR>CR_SERVER_LOST <BR>对服务器的连接在查询期间失去。 <BR>CR_UNKNOWN_ERROR <BR>发生一个未知的错误。 <BR>20.4.18.4 范例<BR><B style='color:black;background-color:#ffff66'>MYSQL</B>_ROW row;<BR>unsigned int num_fields;<BR>unsigned int i;
<P>num_fields = <B style='color:black;background-color:#ffff66'>mysql</B>_num_fields(result);<BR>while ((row = <B style='color:black;background-color:#ffff66'>mysql</B>_fetch_row(result)))<BR>{<BR> unsigned long *lengths;<BR> lengths = <B style='color:black;background-color:#ffff66'>mysql</B>_fetch_lengths(result);<BR> for(i = 0; i < num_fields; i++)<BR> {<BR> printf("[%.*s] ", (int) lengths[i], row[i] ? row[i] : "NULL");<BR> }<BR> printf("\n");<BR>}<BR><BR>20.4.19 <B style='color:black;background-color:#ffff66'>mysql</B>_field_count()<BR>unsigned int <B style='color:black;background-color:#ffff66'>mysql</B>_field_count(<B style='color:black;background-color:#ffff66'>MYSQL</B> *<B style='color:black;background-color:#ffff66'>mysql</B>)
<P>如果你正在
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -