Home  >  Article  >  Database  >  MySQL中的C API

MySQL中的C API

WBOY
WBOYOriginal
2016-06-07 15:52:321391browse

#include my_global.h#include mysql.hint main(int argc, char **argv){ MYSQL *conn; MYSQL_RES *result; MYSQL_ROW row; int num_fields; int i; conn = mysql_init(NULL); mysql_real_connect(conn, localhost, user, passwd, test, 0, NULL, 0); mysql_

#include <my_global.h>
#include <mysql.h>
int main(int argc, char **argv)
{
  MYSQL *conn;
  MYSQL_RES *result;
  MYSQL_ROW row;
  int num_fields;
  int i;

  conn = mysql_init(NULL);
  mysql_real_connect(conn, "localhost", "user", "passwd", "test", 0, NULL, 0);
  mysql_query(conn, "select * from student");
  result = mysql_store_result(conn);
  num_fields = mysql_num_fields(result);
  while ((row = mysql_fetch_row(result)))
  {
      for(i = 0; i <br>
注释:
<p>MYSQL* conn;</p>
conn = mysql_init(NULL);<br>
初始化一个MYSQL结构体,这个结构体就是一个数据库连接句柄。<br>
<p><br>
</p>
<p>mysql_real_connect(conn, "localhost", "user", '"passwd", "test", 0, NULL, 0) ;<br>
</p>
<p>建立一个到mysql数据库的链接。函数参数 链接句柄、主机名、用户、密码、数据库名、端口、unix套接字和客户端标志。<br>
</p>
<p><br>
</p>
<p>mysql_query(conn, "create database testdb");</p>
<p>mysql_query执行指定为“以Null终结的字符串”的SQL查询。<br>
</p>
<p><br>
</p>
<p>MYSQL_RES *result;<br>
</p>
<p>result = mysql_store_result(conn);<br>
</p>
<p>检索完整的结果集至客户端。<br>
</p>
<p><br>
</p>
<p>MYSQL_ROW row;</p>
<p>row = mysql_fetch_row(result)<br>
</p>
<p>获取结果集中的一行</p>
<p> </p>
<p>mysql_num_fields(result);<br>
</p>
<p>返回结果集中的列数</p>
<p><br>
</p>
<p>for(i = 0; i 
</p>
<p>printf("%s ", row[i] ? row[i] : "NULL");</p>
<p>}<br>
</p>
<p>输出一行数据</p>
<p><br>
</p>
<p>mysql_close(conn);<br>
<br>
关闭数据库链接。<br>
</p>
<p><br>
</p>
<p>printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));<br>
</p>
<p>错误处理。mysql_errno返回上次调用的MySQL函数的错误编号。</p>
<p>mysql_error返回上次调用的MySQL函数的错误消息。</p>
<p><br>
</p>
<p>除非作了其他规定,返回指针的函数将返回非Null值,以指明成功,或返回NULL值以指明出错。</p>
<p>返回整数的函数将返回0以指明成功,或返回非0值以指明出错。注意,非0值仅表明这点。</p>
<p>除非在函数描述中作了其他说明,不要对非0值进行测试:<br>
if (result)                   /* correct */<br>
    ... error ...</p>
<p><br>
if (result 
    ... error ...<br>
 <br>
if (result == -1)             /* incorrect */<br>
</p>
<p><br>
</p>
<p><br>
</p>


</mysql.h></my_global.h>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:MySQL参数调优Next article:C#读取Mysql blob字段