linux mysql c api_Linux c 连接处理MYSQL (API方式)

Mysql 安装:

1. 先安装mysql 代码: sudo apt-get install mysql-server mysql-client

2. 再装开发包 代码: sudo apt-get install libmysqlclient15-dev   (注意:如果不安装这个,将找不到mysql.h)

安装完以后,C代码里添加头文件mysql.h 或者找到位置,#include时通常采用绝对路径找到mysql.h.

上例子代码,仅说明原理和接口使用。

[cpp] view plaincopy

#include

#include

#include

#include "/usr/include/mysql/mysql.h"

/*注意哦,上面必须是mysql.h的绝对地址,一般在mysql下的include目录下,仔细看看你的在哪里?*/

int main(int argc, char *argv[])

{

MYSQL my_connection;

int res;

mysql_init(&my_connection);

/*mysql_real_connect(&mysql,host,user,passwd,dbname,0,NULL,0) == NULL)*/

if (mysql_real_connect(&my_connection, "localhost", "root", "xuchuaini","test",0,NULL,CLIENT_FOUND_ROWS))

{

printf("Connection success\n");

//------------------------------------删除-------------------------------------

res = mysql_query(&my_connection, "delete from tc where id1=10");

if (!res)

{

printf("Deleted %lu rows\n",(unsigned long)mysql_affected_rows(&my_connection));

}

else

{

fprintf(stderr, "Delete error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));

}

//------------------------------------增加-------------------------------------

res = mysql_query(&my_connection, "insert into tc values(10,1.23,100,5)");

if (!res)

{

printf("Inserted %lu rows\n",(unsigned long)mysql_affected_rows(&my_connection));

}

else

{

fprintf(stderr, "Insert error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));

}

//------------------------------------修改-------------------------------------

res = mysql_query(&my_connection, "update tc set id1=1000 where id1=10");

if (!res)

{

printf("Updated %lu rows\n",(unsigned long)mysql_affected_rows(&my_connection));

}

else

{

fprintf(stderr, "Update error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));

}

//------------------------------------查询-------------------------------------

MYSQL_RES *res_ptr; //查询结果

MYSQL_ROW sqlrow;   //行结果

res = mysql_query(&my_connection, "select * from tc");

if (!res)

{

res_ptr=mysql_store_result(&my_connection);

if(res_ptr)

{

printf("Retrieved %lu Rows\n",(unsigned long)mysql_num_rows(res_ptr));

while((sqlrow=mysql_fetch_row(res_ptr)))

{

printf("Fetched data...\n");    //这里应该是处理查询结果。

for(int i=0;i

fprintf(stdout,"%s ",sqlrow[i]);       //关键

printf("%d %f %d %d",atoi(sqlrow[0]),atof(sqlrow[1]),atoi(sqlrow[2]),atoi(sqlrow[3]));

printf("\n");

}

if (mysql_errno(&my_connection))

{

fprintf(stderr,"Retrive error:%s\n",mysql_error(&my_connection));

}

}

mysql_free_result(res_ptr);

}

else

{

fprintf(stderr, "Select error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));

}

//-------------------------------------------------------------------------

mysql_close(&my_connection);

}

else

{

fprintf(stderr, "Connection failed\n");

if (mysql_errno(&my_connection))

{

fprintf(stderr, "Connection error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));

}

}

return EXIT_SUCCESS;

}

Code plus:

1.执行参数   gcc $(mysql_config --cflags) mysql.cpp -o mysql $(mysql_config --libs)

补充:我在centos6.6下用上述命令进行编译出现了问题,没有找到mysqlclient的静态库

因此,实际采用了如下,命令:

gcc $(mysql_config --cflags) -L /usr/lib/mysql connect_mysql.c -o mysql $(mysql_config --libs)  -lmysqlclient

2.执行流程:

声明MYSQL -> init() -> connect() -> query() 增删改只写不拿,所以简单。

对于查,先store_result(),再逐个fetch_row(),其中再逐个拿每一个元素。(如有需要,还需要处理游标)

3.系统调用总结:

【MYSQL my_connection】;     //连接

mysql_init(&my_connection);

mysql_real_connect(&my_connection, "localhost", "root", "xuchuaini","test",0,NULL,CLIENT_FOUND_ROWS);

mysql_query(&my_connection, "delete from tc where id1=10");

mysql_affected_rows(&my_connection);

mysql_errno(&my_connection);

mysql_error(&my_connection);

mysql_close(&my_connection);

【MYSQL_RES *res_ptr】;     //接收结果

mysql_num_rows(res_ptr);

*MYSQL_RES *mysql_store_result(MYSQL *connection);  //从服务器检索数据放入客户机

*my_ulonglong mysql_num_rows(MYSQL_RES *result);   //从结果返回实际的行数(mysql_sotre_resule + mysql_num_rows 一起使用)

MYSQL_ROW mysql_fetch_row(MYSQL_RES *result);     //从结果集拿出一行,结束返回NULL。

mysql_num_fields(MYSQL_RES *result);  //列数

void mysql_data_seek(MYSQL_RES *result, my_ulonglong offset);   //这是当前游标,从0到n-1.

*MYSQL_ROW_OFFEST mysql_row_tell(MYSQL_RES *result);   //返回偏移值。不是行号,而是用于:

*MYSQL_ROW_OFFSET mysql_row_seek(MYSQL_RES *result, MYSQL_ROW_OFFSET offset);  //移动到当前位置,并返回以前的值。

void mysql_free_result(MYSQL_RES *result);   //释放

特别注意:row_tell row_seek得到的偏移值[MYSQL_ROW_OFFEST] != 行号[my_ulonglong]。

【MYSQL_ROW sqlrow】;      //接收结果的一行。

本质:“MYSQL_ROW   指针是一简单的字符串数组。所有的数据类型被转换成字符串送到客户端。”很有意义。

需要使用 atoi() 和  atof()将 MYSQL_ROW   字符串中对应int 和 float的值转变为 整型值 和 浮点值。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值