CentOS 6.4系统下使用C语言访问Mysql

用C语言连接MysqL数据库包含两个步骤:

1 初始化一个连接句柄结构

2 实际进行连接


使用MysqL_init来初始化连接句柄

#include <MysqL.h>

MysqL * MysqL_init(MysqL *);

通常你传递NULL给这个例程,它会返回一个指向新分配的连接句柄结构的指针。如果你传递一个已有的结构,它将被重新初始化。这个例程在出错时返回NULL。

什么是句柄?

在程序设计中,句柄是一种特殊的智能指针 。当一个应用程序要引用其他系统(如数据库、操作系统)所管理的内存块或对象时,就要使用句柄。
句柄与普通指针的区别在于,指针包含的是引用对象的内存地址,而句柄则是由系统所管理的引用标识,该标识可以被系统重新定位到一个内存地址上。这种间接访问对象的模式增强了系统对引用对象的控制。

分配和初始化了一个结构,仍需要使用MysqL_real_connect来向一个连接提供参数:

MysqL * MysqL_real_connect(MysqL *connection,

const char * server_host,microsoft yahei">const char * sql_user_name,microsoft yahei">const char * sql_password,microsoft yahei">const char * db_name,microsoft yahei">unsigned int port_number,microsoft yahei">const char * unix_socket_name,microsoft yahei">unsigned int flags,);

connection:指向已经被MysqL_init初始化过的结构

server_host:可以是主机名,也可以是ip地址。如果只是连接到本地机器,你可以通过指定localhost来优化连接类型。

sql_user_name:数据库登录

sql_password:数据库密码

db_name:数据库

port_number:端口号(没改变MysqL认设置,使用0表示认值)

unix_socket_name:为NULL表示认值

flags:用来对一些定义的位模式进行OR操作,使得改变使用协议的某些特性。

void MysqL_close(MysqL *connection);

关闭连接。如果连接是由MysqL_init建立的,MysqL结构会被释放。指针将会失效并无法再次使用。保留一个不需要的连接是对资源的浪费,但是重新打开连接也会带来额外的开销,所以必须自己权衡何时使用这些选项。


MysqL_options(仅能在MysqL_init和MysqL_real_connect之间调用)

int MysqL_options(MysqL *connection,enum option_to_set,const char *argument);

因为MysqL_options一次只能设置一个选项,所以每设置一个选项就得调用它一次。你可以多次调用,只要它出现在MysqL_init和MysqL_real_connect之间即可。

列出3个最常用的选项,如下:

enum选项 实际参数类型 说明
MysqL_OPT_CONNECT_TIMEOUT const unsigned int * 连接超时之前的等待秒数
MysqL_OPT_COMPRESS 使用NULL 网络连接中使用压缩机制
MysqL_INIT_COMMAND const char * 每次连接建立后发送的命令

一次成功的调用返回0。

用户名root和密码yao来连接本机服务器上名为test的数据库

connect.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdlib.h>
#include <stdio.h>
#include "MysqL.h"
int main( argc, char **argv)
{
MysqL * conn_ptr;
conn_ptr = MysqL_init(NULL);
if (!conn_ptr)
{
perror ( "MysqL_init Failed\n" );
exit (1);
}
conn_ptr = MysqL_real_connect(conn_ptr, "localhost" ,monospace!important; font-size:1em!important; min-height:auto!important; color:blue!important; background:none!important">"root" "yao" "test" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,NULL);
(conn_ptr)
{
printf "connection success\n" );
}
else
{
"connection Failed\n" );
}
MysqL_close(conn_ptr);
return 0;
}

编译这个程序,需要同时添加include路径和库文件路径,以及指定链接的库模块MysqLclient。

gcc -I/usr/include/MysqL connect.c -L/usr/lib/MysqL -lMysqLclient -o connect

可能的错误

Centos 6.4 64位系统下使用C语言访问MysqL 找不到MysqLclient


解决

-L/usr/lib/MysqL改为-L/usr/lib64/MysqL


错误处理

MysqL使用一系列由连接句柄结构报告的返回码。

unsigned int MysqL_errno(MysqL * connection);

char * MysqL_error(MysqL * connection);

通过调用MysqL_errno并传递连接结构来获得错误码,通常都是非0值。如果未设定错误码,它将返回0。因为每次调用库都会更新错误码,所以你只能得到最后一个执行命令的错误码。上面两个错误检查函数是例外,它们不会导致错误码的更新。

MysqL_errno的返回值实际上就是错误码,它们在头文件errmsg.h或MysqLd_error.h中定义。这两个文件都可以在MysqL的include目录中找到。前者报告客户端错误,后者关注服务端错误

如果喜欢文本信息错误调用MysqL_error,提供了有意义的文本信息,这些信息被写入一些内部静态内存空间中。

使用非动态分配的连接结构

22
MysqL MysqL_conn;
MysqL_init(&MysqL_conn);
(MysqL_real_connect(&MysqL_conn,monospace!important; font-size:1em!important; min-height:auto!important; color:blue!important; background:none!important">"dfdsfjfd" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,NULL))
{
);
MysqL_close(&MysqL_conn);
}
else
{
);
(MysqL_errno(&MysqL_conn))
{
fprintf (stderr,monospace!important; font-size:1em!important; min-height:auto!important; color:blue!important; background:none!important">"connection error %d:%s\n" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,MysqL_errno(&MysqL_conn),MysqL_error(&MysqL_conn));
}
}
0;
注:这里我使用动态分配的连接结构,错误信息不产生。

执行sql语句

执行sql语句的函数

int MysqL_query(MysqL *connection,const char *query);

这个函数接受连接结构指针和文本字符串形式的有效sql语句(没有结束的分号)。如果成功,返回0.对于包含二进制数据的查询,可以使用第二个函数MysqL_real_query。

1 不返回数据的sql语句

不返回任何数据的sql语句:update delete insert

这里介绍另一个重要函数,用于检查首查询影响的行数

my_ulonglong MysqL_affected_rows(MysqL *connection);

这个函数的返回值类型很不常见,它使用无符号类型。当你使用printf时,推荐使用%lu格式将其转换为无符号长整数。这个函数返回受之前执行的update、insert或delete查询影响的行数。如果你使用过其他sql数据库MysqL返回的是被一个更新操作修改的行数,但许多其他数据库将仅仅因为记录匹配where子句就把它视为已经更新过。

插入一条数据

27
28
29
30
31
32
33
34
res;
MysqL_init(&MysqL_conn);
ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,NULL))
res = MysqL_query(&MysqL_conn,monospace!important; font-size:1em!important; min-height:auto!important; color:blue!important; background:none!important">"insert into child(childid,age,name) values (1,12,'bing')" );
(!res)
{
"inserted %lu rows\n" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,(unsigned long )MysqL_affected_rows(&MysqL_conn));
}
"insert error %d:%s" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,MysqL_error(&MysqL_conn));
MysqL_close(&MysqL_conn);
}
else
{
);
(MysqL_errno(&MysqL_conn))
{
"Connection error %d:%s\n" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,MysqL_error(&MysqL_conn));
}
}
0;

今天先到这,下次继续补充。。。

相关文章

Centos下搭建性能监控Spotlight
CentOS 6.3下Strongswan搭建IPSec VPN
在CentOS6.5上安装Skype与QQ
阿里云基于centos6.5主机VPN配置
CentOS 6.3下配置multipah
CentOS安装、配置APR和tomcat-native