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):
/* Simple C program that connects to MySQL Database server*/
#include <mysql.h>
#include <stdio.h>
main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "PASSWORD"; /* set me first */
char *database = "mysql";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output table name */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
/* close connection */
mysql_free_result(res);
mysql_close(conn);
}
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
分享到:
相关推荐
How To Connect To The FTP Server
Secure Programming for Linux and Unix HOWTO by David A. Wheeler v3.010 Edition Published v3.010, 3 March 2003 Copyright ? 1999, 2000, 2001, 2002, 2003 by David A. Wheeler This book provides a set of ...
这是一本C++的初级教课书,第10版,作者是Paul Deitel and Harvey Deitel
How to connect with VB to Mysql database
演示如何在NetBeansIDE5.5连接SQL Server 2000 本文件为flash
HOWTO: Subversion for Windows with Apache server - a beginner's guide
C++程序设计教学课件:ch2 How to write a program.pdf
PHP and MySQL in easy steps, 2nd edition demonstrates by example how to produce data-driven web pages using the powerful PHP scripting language and the popular free MySQL database server. The book ...
how to program c++ bopu.m, 377 , 2008-05-13 erweihailangboxing.m, 1588 , 2008-05-19 hailangboxing.m, 1222 , 2008-05-18 SDwave.m, 2216 , 2008-04-21 迭加法一维.doc, 83968 , 2008-05-12 叠加法二维....
secure-program-HOWTO
C How to Program is a comprehensive introduction to programming in C. Like other texts of the Deitels’ How to Program series, the book serves as a detailed beginner source of information for college ...
GCC-HOWTO (33KB) 如何安装GNUC编译器和程式库的方法 Glibc2-HOWTO (13KB) 如何在Linux系统上安装与使用第二版 GNU C Library Hardware-HOWTO (42KB) 详列了大部份被 Linux 所支援的硬件和驱动程序 ...
C 程序设计教学课件:ch2 How to write a program.pdf
博文链接:https://tomshen.iteye.com/blog/192140
C How to Program 6th Edition Deitel父子的《C大学教程》
HOWTO-html<br>Kerneld<br>Kernel-HOWTO-html<br>Large-Disk-html<br>LinuxDoc+Emacs+Ispell-HOWTO-html<br>Linux+DOS+Win95<br>Linux+FreeBSD-html<br>Linux+NT-Loader-html<br>META-FAQ-html<br>Modules<br>mul.c...
C++ How to Program, 10th Edition.pdf
C++ How to Program, 8th edition Deitel C How To Program 6th Edition
C How to Program(8th) 英文无水印pdf 第8版 pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者...