`
fenglin
  • 浏览: 31785 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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):

/* 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

    How To Connect To The FTP Server

    Secure Programming for Linux and Unix HOWTO.rtf

    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 ...

    connexion-mysql.zip_How to Connect_MYSQL_mysql vb_vb mysql

    标题中的"connexion-mysql.zip_How to Connect_MYSQL_mysql vb_vb mysql"表明这是一个关于使用VB(Visual Basic)连接MySQL数据库的教程资源包。描述进一步证实了这一点,它提到“如何使用VB连接到MySQL数据库”。从...

    How to connect to SQL Server 2000 in NetBeansIDE5.5

    演示如何在NetBeansIDE5.5连接SQL Server 2000 本文件为flash

    HOWTO: Subversion for Windows with Apache server - a beginner's guide

    HOWTO: Subversion for Windows with Apache server - a beginner's guide

    C++ How to Program, 10th Edition

    The best-selling C++ How to Program is accessible to readers with little or no programming experience, yet comprehensive enough for the professional programmer. The Deitels’ signature live-code ...

    C How to Program 6th Edition

    《C How to Program 6th Edition》是Paul Deitel与Harvey Deitel父子合著的一本经典C语言编程教程,已经更新至第六版。这本书针对初学者和有一定经验的程序员,详细介绍了C语言的基础和高级特性,是学习C语言的重要...

    c++ how to program

    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 叠加法二维....

    DMA-API-HOWTO使用方法1

    DMA-API-HOWTO使用方法1 DMA-API-HOWTO是一份指导设备驱动程序编写者如何使用DMA API的指南,旨在帮助驱动程序编写者了解如何正确地使用DMA API,以便在设备驱动程序中实现动态DMA映射。下面是对DMA-API-HOWTO的...

    Linux Howto 中文文档

    GCC-HOWTO (33KB) 如何安装GNUC编译器和程式库的方法 Glibc2-HOWTO (13KB) 如何在Linux系统上安装与使用第二版 GNU C Library Hardware-HOWTO (42KB) 详列了大部份被 Linux 所支援的硬件和驱动程序 ...

    c how to program

    《C How to Program》是一本深受程序员喜爱的C语言学习指南,特别适合初学者和有经验的程序员作为提升技能的参考书。这本书以其全面、详细和实用的特点,为读者提供了深入理解C语言的基础和进阶知识。彩板设计使得...

    How to connect to remote MB Server using WMBT?

    博文链接:https://tomshen.iteye.com/blog/192140

    linux howto中文合集

    HOWTO-html&lt;br&gt;Kerneld&lt;br&gt;Kernel-HOWTO-html&lt;br&gt;Large-Disk-html&lt;br&gt;LinuxDoc+Emacs+Ispell-HOWTO-html&lt;br&gt;Linux+DOS+Win95&lt;br&gt;Linux+FreeBSD-html&lt;br&gt;Linux+NT-Loader-html&lt;br&gt;META-FAQ-html&lt;br&gt;Modules&lt;br&gt;mul.c...

    C How to Program(8th) 无水印pdf

    C How to Program(8th) 英文无水印pdf 第8版 pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者...

Global site tag (gtag.js) - Google Analytics