用C api 连接mysql数据库的 开发实例
来源:互联网 发布:如何复制淘宝店铺宝贝 编辑:程序博客网 时间:2024/05/21 22:31
1. 下载mysql c connector 安装包; 有两种方法:
1 下载tar文件, 将其中的的 lib, include , bin 都放在 /usr/local 相应的目录下;
2. 也可配置链接库的附加目录 : sudo vi /etc/ld.so.conf 文件中加入你的连接器的lib目录;
3. 好像也可以用 sudo apt-get install libmysqclient-dev
对于c++的好像是 sudo apt-get install libmysql++-dev
2.编辑 C 程序利用mysql c api : 我的程序如下:
3. 编译 链接,测试:
注意链接动态库的选项 : -lmysql
4.对mysql c开发几个有用的命令:ldd, mysql_config, file ,nm
ldd: 输出对动态库的依赖,即依赖的动态库的信息;
mysql_conf :可以获得对mysql的配置开发的信息: mysql_config -- libs
看一下,这几个命令的输出情况:
下文的链接:http://www.cyberciti.biz/tips/linux-unix-connect-mysql-c-api-program.html
Howto: Connect MySQL server using C program API under Linux or UNIX
From my mailbag:
How do I write a C program to connect MySQL database server?
MySQL database does support C program API just like PHP or perl.
The C API code is distributed with MySQL. It is included in the mysqlclient library and allows C programs to access a database.
Many of the clients in the MySQL source distribution are written in C. If you are looking for examples that demonstrate how to use the C API, take a look at these clients. You can find these in the clients directory in the MySQL source distribution.
Requirements
Make sure you have development environment installed such as gcc, mysql development package etc. Following is the list summarize the list of packages to compile program:
- mysql: MySQL client programs and shared library
- mysqlclient: Backlevel MySQL shared libraries (old libs)
- mysql-devel: Files for development of MySQL applications (a must have)
- mysql-server: Mysql server itself
- gcc, make and other development libs: GNU C compiler
Sample C Program
Following instructions should work on any Linux distro or UNIX computer. Here is the small program that connects to mysql server and list tables from mysql database.(download link):
How do I compile and link program against MySQL libs?
MySQL comes with a special script called mysql_config. It provides you with useful information for compiling your MySQL client and connecting it to MySQL database server. You need to use following two options.
Pass --libs option - Libraries and options required to link with the MySQL client library.
$ mysql_config --libs
Output:
-L/usr/lib64/mysql -lmysqlclient -lz -lcrypt -lnsl -lm -L/usr/lib64 -lssl -lcrypto
Pass --cflags option - Compiler flags to find include files and critical compiler flags and defines used when compiling the libmysqlclient library.$ mysql_config --cflags
Output:
-I/usr/include/mysql -g -pipe -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing
You need to pass above option to GNU C compiler i.e. gcc. So to compile above program, enter:$ gcc -o output-file $(mysql_config --cflags)mysql-c-api.c $(mysql_config --libs)
Now execute program:$ ./output-file
Output:
MySQL Tables in mysql database:columns_privdbfunchelp_categoryhelp_keywordhelp_relationhelp_topichosttables_privtime_zonetime_zone_leap_secondtime_zone_nametime_zone_transitiontime_zone_transition_typeuser
References:
- MySQL C API - A must read - official MySQL C API documentation
- 用C api 连接mysql数据库的 开发实例
- 用C api 连接mysql数据库的 开发实例
- ubuntu mysql c api的使用开发实例程序。
- Linux下用C语言API连接MySQL数据库
- Linux下用C语言API连接MySQL数据库
- Linux下用C语言API连接MySQL数据库
- cygwin下用mysql c api连接数据库详解
- C++使用C API 连接MYSQL数据库
- C API 通过ODBC 连接数据库MYSQL
- C API 通过ODBC 连接数据库MYSQL
- 用C API实现MySQL数据库的连接和增删改查
- C语言连接MySQL数据库实例
- C语言连接MySQL数据库实例
- 【数据库开发】 C连接mysql——常用的函数
- C++利用MySQL API连接和操作数据库实例详解
- C/C++通过MySQL的API连接数据库并对数据库中内容RSA解密
- C API连接MYSQL
- Linux下C语言API连接MySQL数据库编译选项
- JQuery(四)
- 递推题目大整理
- 程序员中"5%神话";刘未鹏:为什么你应该写博客
- 一个计算机专业学生几年的编程经验汇总
- 一个计算机专业学生几年的编程经验汇总
- 用C api 连接mysql数据库的 开发实例
- WIN7系统下用VC2008编译OpenCV2.2
- Android的ArrayAdapter
- .Net面试题汇总(一) 帮你轻松过笔试关 --不断增加中
- SQL
- (二)论如何做一个好的主程序 人一口福
- 程序调试的策略
- OSI七层网络模型与TCP/IP四层网络模型
- validationEngine中文版 — jquery强大的表单验证插件