Howto: Connect MySQL server using C program API under Linux or UNIX
来源:互联网 发布:网络电影 微信幽诡 编辑:程序博客网 时间:2024/05/16 09:29
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
- Howto: Connect MySQL server using C program API under Linux or UNIX
- Howto: Connect MySQL server using C program API under Linux or UNIX
- HowTo: Add Jobs To cron Under Linux or UNIX?
- (HTTP) Streaming a video using a program in C/C++ under Linux(轉載)
- simply web server by unix c program
- HOWTO: Receive/Send Multicasts Under WinNT/Win95 Using WinSock
- BASH Shell change the color of my shell prompt under Linux or UNIX
- Using ListView control under Win32 API
- C programs under Linux
- Using GHDL for interactive simulation under Linux
- Howto - Cross Compiling in Linux using MingW32
- HowTo: Use cat Command In Linux / UNIX
- Installing MySQL on Unix/Linux Using Generic Binaries
- Compile 32 bit program under 64 bit Linux OS
- linux or unix 常用命令
- Slow performance occurs when you copy data to a TCP server by using a Windows Sockets API program
- Slow performance occurs when you copy data to a TCP server by using a Windows Sockets API program
- Program Library HOWTO
- java中properties文件的常用读取方法
- XML文档结构
- 推荐分享22个优秀的项目管理与协作工具
- SQLServer存储过程的返回值
- IPC
- Howto: Connect MySQL server using C program API under Linux or UNIX
- session 用户退出
- linux下的super-vlan配置
- 求n = a1*a2*33...*ak 且满足(1<a1<=a2<=a3...<=ak<=n)的状态数
- ARM总线问题
- aa
- java-final关键字
- Oralce函数数字转大写
- The 7th Southwest Jiao Tong University Programming Contest——Problems Easy-Summary