数据库编程基本流程(query)

函数查询地址

​​​​​​MySQL :: MySQL 8.0 C API Developer Guide :: 5.4 C API Basic Function Descriptionsicon-default.png?t=L9C2https://dev.mysql.com/doc/c-api/8.0/en/c-api-function-descriptions.html

1. 初始化数据库 

SQL* mysql_init(MYSQL *mysql);
// An initialized MYSQL* handler. 
// NULL if there was insufficient memory to allocate a new object.

2. 连接数据库

MYSQL *
mysql_real_connect(MYSQL *mysql,                //init返回值
                   const char *host,            //主机名,默认NULL=localhost
                   const char *user,            //用户名
                   const char *passwd,          //密码
                   const char *db,              //数据库名
                   unsigned int port,           //接口,默认0
                   const char *unix_socket,     //套接字,默认NULL
                   unsigned long client_flag);  //标志位,默认0
// A MYSQL* connection handler if the connection was successful
// NULL if the connection was unsuccessful
//For a successful connection, the return value is the same as the value of the first argument.

3. 查询

int mysql_query(MYSQL *mysql,              
            const char *stmt_str);           //查询语句
//Zero for success. Nonzero if an error occurred.

4. 保存

MYSQL_RES *
mysql_store_result(MYSQL *mysql);
//A pointer to a MYSQL_RES result structure with the results. 
//NULL if the statement did not return a result set or an error occurred. 
//检错: mysql_errno() returns nonzero, or mysql_field_count() returns zero.

5. 打印查询结果

// 获取行数
uint64_t mysql_num_rows(MYSQL_RES *result);
//Returns the number of rows in the result set

//检索
MYSQL_ROW mysql_fetch_row(MYSQL_RES *result);
//成功返回下一列数据对应的结构体

/*When used after mysql_store_result() or mysql_store_result_nonblocking(), 
mysql_fetch_row() returns NULL if there are no more rows to retrieve.

When used after mysql_use_result(), mysql_fetch_row() returns NULL if there 
are no more rows to retrieve or an error occurred. To determine whether an 
error occurred, check whether mysql_error() returns a nonempty string or 
mysql_errno() returns nonzero.*/

//遍历
unsigned int mysql_num_fields(MYSQL_RES *result);
//Returns the number of columns in a result set.

6. 释放查询结果

void mysql_free_result(MYSQL_RES *result);

7. 断开连接

void mysql_close(MYSQL *mysql);

NOTICE: 编译时链接动态库 -lmysqlclient

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值