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)) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      );
   }
   /* send SQL query */
   if (mysql_query(conn, "show tables")) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      );
   }
   res = mysql_use_result(conn);
   /* output table name */
   printf("MySQL Tables in mysql database:\n");
   while ((row = mysql_fetch_row(res)) != NULL)
      ]);
   /* 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 

Howto: Connect MySQL server using C program API under Linux or UNIX的更多相关文章

  1. (二)阿里云ECS Linux服务器外网无法连接MySQL解决方法(报错2003- Can't connect MySQL Server on 'x.x.x.x'(10038))(自己亲身遇到的问题是防火墙的问题已经解决)

    我的服务器买的是阿里云ECS linux系统.为了更好的操作数据库,我希望可以用navicat for mysql管理我的数据库. 当我按照正常的模式去链接mysql的时候, 报错提示: - Can' ...

  2. ECSHOP info: Can't Connect MySQL Server(localhost:3306)!

    今天对服务器压力测试后,某个公司电脑的页面显示标题所示,别人电脑正常.故让她清理cookie.无效.重启MySQL后问题解决.

  3. 最近使用Navicat for MySQl访问远程mysql数据库,出现报错,显示“2003- Can't connect MySQL Server on 'localhost'(10038)“。

    优先考虑mysql数据库是否开启 1.先看报错窗口.   通过百度,最终找到的原因是:远程3306端口未对外开放. 于是下面进行远程3306端口开放操作.   首先远程连接服务器,点击“开始”-“管理 ...

  4. Mysql is not allowed to connect mysql server

    1.     mysql -u root -p 2.    select host from user where user='root';      //可以看到当前主机配置信息为localhost ...

  5. MySQL记录-Lost Connect MySQL Server during query解决方案

    vim /etc/my.cnf ,在[mysqld]下面加上: skip-name-resolve max_allowed_packet = 800M default-character-set=ut ...

  6. mysql server advanced 5.6基于oracle linux 6.6的安装

    mysql 安装有两种,rpm安装和源码包安装,两种包都可以从www.mysql.com官网下载,这次我测试下rpm安装方式. 1.安装环境以及mysql版本: 1.1vcenter 虚拟机环境 1. ...

  7. ubuntu 16.04安装mysql server入门

    1.安装mysql-server -> sudo apt-get install mysql-server 输入root密码即可 2.修改服务器配置 默认mysql-server只对本机访问,新 ...

  8. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

    有时候,当我们使用"mysql"."mysqladmin"."mysqldump"等命令管理数据库时,服务器抛出类似如下错误: 一.错误现场 ...

  9. MySql数据库:Host 'localhost' is not allowed to connect to this MySQL server

    修改mysql的root密码后,出现Host 'localhost' is not allowed to connect to this MySQL server 错误. 解决办法: C:\Progr ...

随机推荐

  1. 在OpenResty中使用lua-zlib的方法

    ================================================================== 1.查看 zlib在centos 中是否存在? rpm -qa | ...

  2. Access an instance through a console

    VNC or SPICE is used to view the console output of an instance, regardless of whether or not the con ...

  3. 学 Redux (转)

    0.官方文档:http://redux.js.org/ 中文版本 :  http://cn.redux.js.org/ 中文版本(好!):   http://www.css88.com/react/d ...

  4. 【转】mysql的cardinality异常,导致索引不可用

    转自:http://ourmysql.com/archives/1343 前段时间,一大早上,就收到报警,警告php-fpm进程的数量超过阈值.最终发现是一条sql没用到索引,导致执行数据库查询慢了, ...

  5. Qt不同类之间信号槽连接

    1.类必须继承QObject. #ifndef TESTA_H #define TESTA_H #include <QObject> class TestA : public QObjec ...

  6. effective OC2.0 52阅读笔记(五 内存管理)

    第五章:内存管理 29 理解引用计数 30 以ARC简化引用计数 总结:ARC通过命名约定将内存管理规则标准化.其他编程语言很少像OC这样强调命名.ARC通过设置全局数据结构(此数据结构的具体内容因处 ...

  7. Java for LeetCode 217 Contains Duplicate

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  8. json格式

    $.post('text.action',{....},function(datas){ var name=datas.data[0].name; }); 如果是多个还可以用循环获取.$.post(' ...

  9. PLSQL

    select t.*,rowid from Table t; 编辑器设置

  10. form、iframe实现异步上传文件

    转载自:http://blog.csdn.net/sunjing21/article/details/4779321 实现主要功能: 页面提供一个上传图片的input file选择框,用于上传某一类型 ...