//mysqlVersion.c
[cpp] view plain copy print ?
-
-
-
-
-
-
-
-
- #include <stdio.h>
- #include <mysql.h>
-
- void mysql_version( void )
- {
- printf("MySQL client version : %s ! \n",mysql_get_client_info());
- }
-
- int main(int argc,char *argv[])
- {
- mysql_version();
-
- return 0;
- }
/*************************************************************
FileName : mysqlVersion.c
FileFunc : 显示MySQL客户端版本
Version : V0.1
Author : Sunrier
Date : 2012-04-19
Descp : Linux下使用C语言访问MySQL函数
*************************************************************/
#include <stdio.h>
#include <mysql.h>
void mysql_version( void )
{
printf("MySQL client version : %s ! \n",mysql_get_client_info());
}
int main(int argc,char *argv[])
{
mysql_version();
return 0;
}
//makefile
[cpp] view plain copy print ?
- #makefile开始
- demo:mysqlVersion.c
- @gcc -I/usr/include/mysql mysqlVersion.c -o demo -L/usr/lib/mysql -lmysqlclient
- clean :
- @ls | grep -v ^makefile$$ | grep -v [.]c$$ | grep -v [.]h$$ | grep -v [.]sql$$ | xargs rm -rf
- #makefile结束
#makefile开始
demo:mysqlVersion.c
@gcc -I/usr/include/mysql mysqlVersion.c -o demo -L/usr/lib/mysql -lmysqlclient
clean :
@ls | grep -v ^makefile$$ | grep -v [.]c$$ | grep -v [.]h$$ | grep -v [.]sql$$ | xargs rm -rf
#makefile结束
[Sunrier@localhost MySql]$ make
[Sunrier@localhost MySql]$ ./demo
MySQL client version : 5.0.22 !
[Sunrier@localhost MySql]$
//demo.c
[cpp] view plain copy print ?
-
-
-
-
-
-
-
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <mysql.h>
-
- int main(int argc,char *argv[])
- {
- MYSQL my_connection,*conn_ptr;
- MYSQL_RES *res_ptr;
- MYSQL_ROW sqlrow;
- int iRet;
- int iTableRow,iTableCol,i,j;
- char *server = "localhost";
- char *user = "Sunrier";
- char *password = "sakila";
- char *database = "mysql";
- unsigned int uiTimeOut = 7;
-
- conn_ptr = mysql_init(&my_connection);
- if( !conn_ptr )
- {
- fprintf(stderr,"mysql_init failed ! \n");
- return EXIT_FAILURE;
- }
-
- iRet = mysql_options(&my_connection,MYSQL_OPT_CONNECT_TIMEOUT,(const char *)&uiTimeOut);
- if( iRet )
- {
- fprintf(stderr,"Connection is timeout! \n");
- return EXIT_FAILURE;
- }
-
- conn_ptr = mysql_real_connect(&my_connection,server,user,password,database,0,NULL,0);
- if( conn_ptr )
- {
- printf("Connection success!\n");
-
- iRet = mysql_query(&my_connection,"select * from children");
- if( iRet )
- {
- fprintf(stderr,"select error %d: %s !\n",mysql_errno(&my_connection),mysql_error(&my_connection));
- return EXIT_FAILURE;
- }
-
-
- res_ptr = mysql_store_result(&my_connection);
- if( res_ptr )
- {
- iTableRow = mysql_num_rows(res_ptr);
- iTableCol = mysql_num_fields(res_ptr);
-
- for(i=0; i<iTableRow; i++)
- {
- sqlrow = mysql_fetch_row(res_ptr);
- for(j=0; j<iTableCol; j++)
- {
- printf("%-8s ",sqlrow[j]);
- }
- printf("\n");
-
- }
-
- mysql_free_result(res_ptr);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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;
-
- }
/*************************************************************
FileName : demo.c
FileFunc : C语言接口访问MySQL
Version : V0.1
Author : Sunrier
Date : 2012-04-19
Descp : Linux下使用C语言访问MySQL函数
*************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
int main(int argc,char *argv[])
{
MYSQL my_connection,*conn_ptr;
MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;
int iRet;
int iTableRow,iTableCol,i,j;
char *server = "localhost";
char *user = "Sunrier";
char *password = "sakila";
char *database = "mysql";
unsigned int uiTimeOut = 7;//设置连接超时7s
conn_ptr = mysql_init(&my_connection);//初始化连接句柄
if( !conn_ptr )
{
fprintf(stderr,"mysql_init failed ! \n");
return EXIT_FAILURE;
}
iRet = mysql_options(&my_connection,MYSQL_OPT_CONNECT_TIMEOUT,(const char *)&uiTimeOut);//设置连接超时
if( iRet )
{
fprintf(stderr,"Connection is timeout! \n");
return EXIT_FAILURE;
}
conn_ptr = mysql_real_connect(&my_connection,server,user,password,database,0,NULL,0);//连接数据库
if( conn_ptr )
{
printf("Connection success!\n");
iRet = mysql_query(&my_connection,"select * from children");//执行SQL语句
if( iRet )
{
fprintf(stderr,"select error %d: %s !\n",mysql_errno(&my_connection),mysql_error(&my_connection));//打印错误处理具体信息
return EXIT_FAILURE;
}
res_ptr = mysql_store_result(&my_connection);//集合
if( res_ptr )
{
iTableRow = mysql_num_rows(res_ptr);//行
iTableCol = mysql_num_fields(res_ptr);//列
for(i=0; i<iTableRow; i++)
{
sqlrow = mysql_fetch_row(res_ptr);
for(j=0; j<iTableCol; j++)
{
printf("%-8s ",sqlrow[j]);//字符串向左靠,右补空格
}
printf("\n");
}
mysql_free_result(res_ptr);//完成对数据的所有操作后,调用此函数来让MySQL库清理它分配的对象
}
/*
res_ptr = mysql_use_result(&my_connection);//行
if( res_ptr )
{
while( (sqlrow = mysql_fetch_row(res_ptr)) )
{
//iTableCol = mysql_num_fields(res_ptr);//列
iTableCol = mysql_field_count(&my_connection);//和上面一句等价
for(j=0; j<iTableCol; j++)
{
printf("%-8s ",sqlrow[j]);//字符串向左靠,右补空格
}
printf("\n");
}
mysql_free_result(res_ptr);
}
*/
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;
}
//makefile
[cpp] view plain copy print ?
- #makefile
- demo:demo.c
- @gcc -I/usr/include/mysql demo.c -L/usr/lib/mysql -lmysqlclient -o demo
- clean :
- @rm -rf demo
#makefile
demo:demo.c
@gcc -I/usr/include/mysql demo.c -L/usr/lib/mysql -lmysqlclient -o demo
clean :
@rm -rf demo
相关知识:
当然在写MySQL客户端的程序已经需要安装了下面两个软件包,我的是VM下Red Hat Enterprise Linux 5,此前已经默认安装了
mysql-server-5.0.22-2.1.0.1.i386.rpm MySQL服务器端
mysql-client-5.0.22-2.1.0.1.i386.rpm MySQL客户端
如果在make的时候报下面类似的错误:
xxx.c:line:错误:mysql.h:没有那个文件或目录
先看看系统中所带的MySQL版本,命令如下:
[root@localhost MySql]# rpm -qa | grep mysql
mysql-server-5.0.22-2.1.0.1
mysql-connector-odbc-3.51.12-2.2
mysql-devel-5.0.22-2.1.0.1
mysql-5.0.22-2.1.0.1
libdbi-dbd-mysql-0.8.1a-1.2.2
[root@localhost MySql]#
如果在上面的显示信息中你没有mysql-devel-xxx-xxx(我已经安装了mysql-devel-5.0.22-2.1.0.1)的话,你需要安装对应版本的mysql-devel-version-release.architecture.rpm这个软件包为了所需的库和包含文件,如果你想要编译其他MySQL客户程序, 例如Perl模块。现在你写C语言的MySQL客户程序,现在就需要安装这个软件包。
先把用户切换到root用户下
1)查询下MySql版本
[root@localhost MySql]# rpm -qa mysql
mysql-5.0.22-2.1.0.1
[root@localhost MySql]#
2)到官网下载到相应版本后,如mysql-devel-5.0.22-2.1.0.1.i386.rpm
3)安装mysql-devel-version-release.architecture.rpm软件包
[root@localhost MySql]# rpm -ivh mysql-devel-5.0.22-2.1.0.1.i386.rpm
[root@localhost MySql]#
4)重新编译链接文件后,看下是否有了mysql-devel打头的文件信息
[root@localhost MySql]# rpm -qa | grep mysql
mysql-server-5.0.22-2.1.0.1
mysql-connector-odbc-3.51.12-2.2
mysql-devel-5.0.22-2.1.0.1
mysql-5.0.22-2.1.0.1
libdbi-dbd-mysql-0.8.1a-1.2.2
[root@localhost MySql]#
其他rpm命令相关的内容
删除rpm软件包
[root@localhost MySql]# rpm -e packagename.rpm
[root@localhost MySql]#
如果在删除的过程中提示因为一些依赖关系无法删除,请用如下命令:
[root@localhost MySql]# rpm -e --nodeps packagename.rpm
[root@localhost MySql]#
查看mysql.h文件的路径(切换到root用户下)
[root@localhost MySql]# find / -name mysql.h | more
/usr/include/mysql/mysql.h
[root@localhost MySql]#
-ivh:安装显示安装进度--install--verbose--hash
-Uvh:升级软件包--Update;
-qpl:列出RPM软件包内的文件信息[Query Package list];
-qpi:列出RPM软件包的描述信息[Query Package install package(s)];
-qf :查找指定文件属于哪个RPM软件包[Query File];
-Va :校验所有的RPM软件包,查找丢失的文件[View Lost];
-e :删除包--erase
-qa: 查找相应文件,如 rpm -qa mysql
--test:安装测试,并不实际安装
--nodeps:忽略软件包的依赖关系强行安装
--force:忽略软件包及文件的冲突