Howto:connect MySQL Server using C program API under Linux or UNIX

Source: Internet
Author: User
Tags mysql client

From my mailbag:

How does 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 is written in C. If you is looking for examples so demonstrate how to with 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 are 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)
    • mysql-server: MySQL server itself
    • GCC, make and other development libs: GNU C Compiler
Sample C Program

Following instructions should work in any Linux distro or UNIX computer. Here are the small program, connects to MySQL server and list tables from MySQL database. (Download link):

/* Simple C program, 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 does 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 the 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 COM Piling 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-compile above program, enter:
gcc -o output-file $(mysql_config --cflags) mysql-c-api.c $(mysql_config --libs)
Now Execute Program:
$ ./output-file
Output:

Howto:connect MySQL Server using C program API under Linux or UNIX

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.