用C api 連線mysql資料庫的 開發例項
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
看一下,這幾個命令的輸出情況:
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_priv db func help_category help_keyword help_relation help_topic host tables_priv time_zone time_zone_leap_second time_zone_name time_zone_transition time_zone_transition_type user
References:
- MySQL C API - A must read - official MySQL C API documentation