原创归andxu所有,转载请保留本链接。
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.
上例子代码,仅说明原理和接口使用。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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<mysql_num_fields(res_ptr);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)
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的值转变为 整型值 和 浮点值。