mysql API的保姆总结

mysql的初始化

mysql_init的使用

错误使用:

MYSQl *mysql = NULL;
mysql_init(mysql);

mysql_init()在参数中传入NULL,然后使用MYSQL *myql来接受函数返回

MYSQL *mysql = mysql_init(NULL);

mysql_init函数应该会在自己的函数内构建一个MYSQl 类型,并对改类型进行了内存分配,所以使用一个MYSQL结构体接受即可。

#include <stdio.h>
#include <stdlib.h>
#include "mysql.h"

int main()
{
    MYSQL *mysql = mysql_init(NULL);
    if(mysql == NULL)
    {
        pinrtf("mysql_init error!\n");
        return -1;
    }
    printf("mysql_init succec!\n");
    return 0;
}

mysql的链接

使用函数

mysql_real_connect()

#include <stdio.h>
#include <stdlib.h>
#include "mysql.h"

int main()
{
    MYSQL *mysql = mysql_init(NULL);
    if(mysql == NULL)
    {
        perror("error!\n");
        return -1;
    }
    printf("mysql_init ok!\n");
    MYSQl *conn = mysql_real_connect(mysql,"127.0.0.1","root","8","tset",0,NULL,0);

    if(conn == NULL)
    {
        printf("the mysql_real_connect error!\n");
        return -1;
    }
    printf("the mysql_real_connet is right!\n");
    return 0;
}

mysql的获取集

mysql_store_result(MYSQL *mysql);失败返回NULL,成功返回指针;
select *from riji;

获取结构集
mysql_qurey(conn,SQL);
MYSQL_RES *mysql_store_result(MYSQL *mysql);
MYSQL_RES *results = mysql_store_result(conn);
if(result == NULL)
{
	printf("mysql_store_result error!\n");
	return -1;
}
printf("mysql_store_result ok!\n");

//获取结果集中的每一行记录
MYSQL_ROW row;
while(row = mysql_fetch_row(results))
{
	printf("%s\t%s\n",row[0],row[1]);
}

//释放结果集
mysql_free_result(MYSQL_RES *result);

使用获取列数函数

获取列数:
从MySQL句柄中获取有多少列
unsigned int mysql_field_count(MYSQL *mysql);
unsigned int num = mysql_filed_count(mysql);
while(row = mysql_fetch_row(results))
{
	for(i = 0;i < num;i++)
	{
		printf("%s\t",row[i]);
	}
	printf("\n");
}

unsigned int mysql_num_fields(MYSQL_RES *results);
unsigned int num = mysql_num_fields(results);

获取表头

获取表头
MYSQL_FIELD* mysql_fetch_fields(MYSQL_RES *result);//全部取出
MYSQL_FIELD* mysql_fetch_field(MYSQL_RES *result);//获取单个

最后的杂记

student@student-machine:~/mysql_test$ gcc -o sql mysql_init.c -I /usr/include/mysql -L /usr/bin/mysql -lmysqlclient
student@student-machine:~/mysql_test$ ./sql 
the mysql_init succece!
the connect right!
mysql_query ok!
the mysql_store_results ok!
---------into mysql table riji----------
(null)	Alice	01
(null)	god	good
1997-01-01	liyong	l
1998-03-19	luoting	Brithday
1998-03-19	luoting	Brithday
1998-03-19	luoting	Brithday
---------end---------
student@student-machine:~/mysql_test$ cat mysql_init.c 
#include <stdio.h>
#include <stdlib.h>
#include "mysql.h"

int main()
{
	//MYSQL mysql;
	MYSQL *mysql =mysql_init(NULL);
	//mysql_init(&mysql);
	if(mysql == NULL)
	{
		printf("the mysql_init error!\n");
		return -1;
	}
	printf("the mysql_init succece!\n");

	//connect mysql
	MYSQL *conn = mysql_real_connect(mysql,"127.0.0.1","root","8","tset",0,NULL,0);
	if(conn == NULL)
		{
			printf("the mysql connect error!\n");
			return -1;
		}
	printf("the connect right!\n");

	//mysql_query
	char SQL[256] = "insert into riji values('1998.3.19','luoting','Brithday')";

	int ret = mysql_query(conn,SQL);
	if(ret != 0)
	{
		printf("mysql_query error!\n");
		return -1;
	}
	printf("mysql_query ok!\n");

	//get mysql table
	char SQL1[256] = "select *from riji";
	mysql_query(conn,SQL1);

	MYSQL_RES *results = mysql_store_result(conn);
	if(results == NULL)
		{
			printf("the mysql_stire_result error!\n");
			return -1;
		}
	printf("the mysql_store_results ok!\n");

	MYSQL_ROW row;
	printf("---------into mysql table riji----------\n");
	while(row = mysql_fetch_row(results))
	{
		//printf("------------into mysql tables riji-----------\n");
		printf("%s\t%s\t%s\n",row[0],row[1],row[2]);
	}
	printf("---------end---------\n");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘多拉的面

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值