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;
}