1、首先当然是连接数据库,函数原型如下:
MYSQL * STDCALL mysql_real_connect(MYSQL *mysql, const char *host,
const char *user,
const char *passwd,
const char *db,
unsigned int port,
const char *unix_socket,
unsigned long clientflag);
第一个参数 MYSQL是 C api中一个非常重要的变量,里面内存非常丰富,有port,dbname,charset等连接基本参数。它也包含了一个叫 st_mysql_methods的结构体变量,该变量里面保存着很多函数指针,这些函数指针将会在数据库连接成功以后的各种数据操作中被调用。mysql_real_connect函数中各参数,基本都是顾名思意。
2、连接数据库成功之后就可以执行sql语句了使用mysql_query
int STDCALL mysql_query(MYSQL *mysql, const char *q);
第一个参数上面已经介绍过,第二个参数为要执行的sql语句。
这个函数总体就两步:
(1)发送sql语句,其实就一个socket发送sql 语句,加上mysql固定的协议头。懒的去看源码了,抓了下包,如下:
0000 19 00 00 00 0373 65 6c 65 63 74 20 61 70 70 5f .....select app_
0010 6e 61 6d 65 20 66 72 6f 6d 20 61 70 70 name from app
红色部分是协议,前面两位其实就是包的长度。具体协议没研究过。
(2)然后就是接受结果,这里将会调用MYSQL变量中的st_mysql_methods中的read_query_result函数指针
3、获取结果
sql执行完以后,如果是查询语句,我们当然还要读取数据,如果update,insert等语句,那么就看下操作成功与否即可。我们来看看如何获取查询结果: 如果mysql_query返回成功,那么我们就通过mysql_store_result这个函数来读取结果。原型如下:
MYSQL_RES * STDCALL mysql_store_result(MYSQL *mysql);
该函数会调用MYSQL变量中的st_mysql_methods中的 read_rows函数指针来获取查询的结果。同时该函数会返回MYSQL_RES 这样一个变量,该变量主要用于保存查询的结果。同时该函数malloc了一片内存空间来存储查询过来的数据,所以我们一定要记的 free(result),不然是肯定会造成内存泄漏的。 执行完mysql_store_result以后,其实数据都已经在MYSQL_RES 变量中了,下面的api基本就是读取MYSQL_RES 中的数据。例如mysql_fetch_row这个函数,就是读去查询结果的一行。函数原型如下
MYSQL_ROW STDCALL mysql_fetch_row(MYSQL_RES *result);
它会返回一个MYSQL_ROW变量,MYSQL_ROW其实就是char **.就当成一个二维数组来用吧。还有很多api,不再一一介绍,大部分信息都在MYSQL_RES MYSQL这两个结构体中。具体可以参考mysql官方网站:http://dev.mysql.com/doc/refman/5.1/en/c.html 突然发现官方网站资料好全面,貌似比任何书都要好。
下面来个例子:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<mysql/mysql.h>
#define MAX_COLUMN_LEN 32
int main(int argc , char *argv[])
{
MYSQL my_connection;
MYSQL_RES *result;
MYSQL_ROW sql_row;
MYSQL_FIELD *fd;
char column[MAX_COLUMN_LEN][MAX_COLUMN_LEN];
int res;
mysql_init(&my_connection);
if(mysql_real_connect(&my_connection,"127.0.0.1","用户","密码","数据名称",3306,NULL,0))
{
perror("connect");
res=mysql_query(&my_connection,"select * from app");//查询
if(!res)
{
result=mysql_store_result(&my_connection);//保存查询到的数据到result
if(result)
{
int i,j;
printf("the result number is %lu\n ",(unsigned long)mysql_num_rows(result));
for(i=0;fd=mysql_fetch_field(result);i++)//获取列名
{
bzero(column[i],sizeof(column[i]));
strcpy(column[i],fd->name);
}
j=mysql_num_fields(result);
for(i=0;i<j;i++)
{
printf("%s\t",column[i]);
}
printf("\n");
while(sql_row=mysql_fetch_row(result))//获取具体的数据
{
for(i=0;i<j;i++)
{
printf("%s\t",sql_row[i]);
}
printf("\n");
}
}
}
else
{
perror("select");
}
}
else
{
perror("connect:error");
}
mysql_free_result(MYSQL_RES *result);//释放结果资源
mysql_close(&my_connection);//断开连接
}
上面这个例子就是从一个表中查数据,然后输出。 如果要insert或者update,只需要修改具体的sql既可。具体的操作都是通过mysql_query这个函数来搞定。 现在来讲编译的方法吧,这里我们需要.h以及.so库。我们可以在http://dev.mysql.com/downloads/connector/c/6.0.html 下载Connector/C。简单的方法就是: 把里面include的东西拷贝到/usr/include/mysql/下面去,这样编译的时候就不需要加-I了,然后把lib下面的东西拷贝的/usr/lib/下去。
gcc具体的编译方法:gcc ***.c -o *** -lmysqlclient
1,select查询的基本结构
- select 字段
- from 表
- where 过滤条件
- group by 分组条件
- order by 排序条件
- having 过滤的第二条件
- limit 限定结果条件;
- desc 倒序排列
2,最简单的查询语句
- mysql> select * from user;
- +----+----------+----------+---------------------+
- | id | username | password | createtime |
- +----+----------+----------+---------------------+
- | 1 | kenan | kenan | 2012-10-31 15:32:26 |
- | 3 | kenan | lele | 2012-10-31 15:32:26 |
- +----+----------+----------+---------------------+
3,查询一列或多列
- mysql> select username,createtime from user;
- +----------+---------------------+
- | username | createtime |
- +----------+---------------------+
- | kenan | 2012-10-31 15:32:26 |
- | kenan | 2012-10-31 15:32:26 |
- +----------+---------------------+
- 2 rows in set (0.00 sec)
4,多表查询
- mysql> select * from user;
- +----+----------+----------+---------------------+
- | id | username | password | createtime |
- +----+----------+----------+---------------------+
- | 1 | kenan | kenan | 2012-10-31 15:32:26 |
- | 3 | kenan | lele | 2012-10-31 15:32:26 |
- +----+----------+----------+---------------------+
- 2 rows in set (0.00 sec)
- mysql> select * from teacher;
- +----+------------+---------------------+
- | id | username | createtime |
- +----+------------+---------------------+
- | 1 | Teacher Li | 2012-11-01 21:00:58 |
- | 2 | Teacher Le | 2012-11-01 21:02:07 |
- +----+------------+---------------------+
- 2 rows in set (0.00 sec)
- mysql> select * from user,teacher;
- +----+----------+----------+---------------------+----+------------+---------------------+
- | id | username | password | createtime | id | username | createtime |
- +----+----------+----------+---------------------+----+------------+---------------------+
- | 1 | kenan | kenan | 2012-10-31 15:32:26 | 1 | Teacher Li | 2012-11-01 21:00:58 |
- | 3 | kenan | lele | 2012-10-31 15:32:26 | 1 | Teacher Li | 2012-11-01 21:00:58 |
- | 1 | kenan | kenan | 2012-10-31 15:32:26 | 2 | Teacher Le | 2012-11-01 21:02:07 |
- | 3 | kenan | lele | 2012-10-31 15:32:26 | 2 | Teacher Le | 2012-11-01 21:02:07 |
- +----+----------+----------+---------------------+----+------------+---------------------+
- 4 rows in set (0.00 sec)
- 多表查询中,如果没有限制条件的话,两个表的记录会分别进行匹配,产生的结果就是两个表的笛卡尔积,叫做全连接
- 下面的这个带有条件的叫做等同连接,相当于在上面的查询结果中挑选出来满足where后面条件的记录,因为没有所有是空的结果。
- mysql> select * from user,teacher where user.username = teacher.username;
- Empty set (0.00 sec)
5,带有简单的where字句条件查询
- mysql> select * from user where id>2;
- +----+----------+----------+---------------------+
- | id | username | password | createtime |
- +----+----------+----------+---------------------+
- | 3 | kenan | lele | 2012-10-31 15:32:26 |
- +----+----------+----------+---------------------+
- 1 row in set (0.00 sec)
6,带有in的查询,in用来表示范围
这里查询出来username是kenan或者是Micheal的记录
- mysql> select * from user where username in ('kenan','Micheal');
- +----+----------+----------+---------------------+
- | id | username | password | createtime |
- +----+----------+----------+---------------------+
- | 1 | kenan | kenan | 2012-10-31 15:32:26 |
- | 3 | kenan | lele | 2012-10-31 15:32:26 |
- +----+----------+----------+---------------------+
- 2 rows in set (0.00 sec)
7,between and 用来表示一个字段的范围的查询
这里分别查询出来id在2和3之间的记录,3到4之间的记录,从结果可以看出 between and是包含边界的,就是>=2而且<=3
- mysql> select * from user where id between 2 and 3;
- +----+----------+----------+---------------------+
- | id | username | password | createtime |
- +----+----------+----------+---------------------+
- | 3 | kenan | lele | 2012-10-31 15:32:26 |
- +----+----------+----------+---------------------+
- 1 row in set (0.00 sec)
- mysql> select * from user where id between 3 and 4;
- +----+----------+----------+---------------------+
- | id | username | password | createtime |
- +----+----------+----------+---------------------+
- | 3 | kenan | lele | 2012-10-31 15:32:26 |
- +----+----------+----------+---------------------+
- 1 row in set (0.00 sec)
8, is null 查询空值
9,用and连接多条件查询 并关系
- mysql> select * from user ;
- +----+----------+----------+---------------------+
- | id | username | password | createtime |
- +----+----------+----------+---------------------+
- | 1 | kenan | kenan | 2012-10-31 15:32:26 |
- | 3 | kenan | lele | 2012-10-31 15:32:26 |
- | 4 | lele | lele | NULL |
- +----+----------+----------+---------------------+
- 3 rows in set (0.00 sec)
- mysql> select * from user where createtime is null;
- +----+----------+----------+------------+
- | id | username | password | createtime |
- +----+----------+----------+------------+
- | 4 | lele | lele | NULL |
- +----+----------+----------+------------+
- 1 row in set (0.00 sec)
- mysql> select * from user where username = 'kenan' and password = 'kenan';
- +----+----------+----------+---------------------+
- | id | username | password | createtime |
- +----+----------+----------+---------------------+
- | 1 | kenan | kenan | 2012-10-31 15:32:26 |
- +----+----------+----------+---------------------+
- 1 row in set (0.00 sec)
10,用or 的多条件查询 这个是或关系,满足一个条件即可
- mysql> select * from user where username = 'kenan' or password = 'kenan';
- +----+----------+----------+---------------------+
- | id | username | password | createtime |
- +----+----------+----------+---------------------+
- | 1 | kenan | kenan | 2012-10-31 15:32:26 |
- | 3 | kenan | lele | 2012-10-31 15:32:26 |
- +----+----------+----------+---------------------+
- 2 rows in set (0.00 sec)
11,聚合函数 count()用来统计总数
- mysql> select count(*) from user;
- +----------+
- | count(*) |
- +----------+
- | 3 |
- +----------+
- 1 row in set (0.00 sec)
12,使用sum()求和,使用avg函数求平均值
- mysql> select id from user;
- +----+
- | id |
- +----+
- | 1 |
- | 3 |
- | 4 |
- +----+
- 3 rows in set (0.00 sec)
- mysql> select sum(id) from user;
- +---------+
- | sum(id) |
- +---------+
- | 8 |
- +---------+
- 1 row in set (0.00 sec)
- mysql> select avg(id) from user;
- +---------+
- | avg(id) |
- +---------+
- | 2.6667 |
- +---------+
- 1 row in set (0.04 sec)
13,使用max()求最大值,使用min求最小值
- mysql> select id from user;
- +----+
- | id |
- +----+
- | 1 |
- | 3 |
- | 4 |
- +----+
- 3 rows in set (0.00 sec)
- mysql> select max(id) from user;
- +---------+
- | max(id) |
- +---------+
- | 4 |
- +---------+
- 1 row in set (0.00 sec)
- mysql> select min(id) from user;
- +---------+
- | min(id) |
- +---------+
- | 1 |
- +---------+
- 1 row in set (0.00 sec)