設為首頁收藏本站

Hippies

 找回密碼
 立即註冊

掃一掃,訪問微社區

搜索
熱搜: 活動 交友 discuz
查看: 2082|回復: 2
打印 上一主題 下一主題

Connect MySQL server using C program API under Linux

[複製鏈接]
  • TA的每日心情
    開心
    2016-2-27 23:29
  • 簽到天數: 27 天

    [LV.4]偶爾看看III

    438

    主題

    611

    帖子

    705

    積分

    高級會員

    Rank: 4

    積分
    705
    跳轉到指定樓層
    樓主
    發表於 2015-2-7 11:19:57 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式 简体中文繁體中文

    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.
    RequirementsMake 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 ProgramFollowing 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 -lcryptoPass --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-aliasingYou 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


    最近訪問 頭像模式 列表模式
  • TA的每日心情
    慵懶
    2016-6-11 10:25
  • 簽到天數: 3 天

    [LV.2]偶爾看看I

    48

    主題

    102

    帖子

    729

    積分

    高級會員

    Rank: 4

    積分
    729

    活躍會員

    沙發
    發表於 2015-2-12 15:26:10 | 只看該作者 简体中文繁體中文
    本帖最後由 blackjack 於 2015-2-12 16:19 編輯

    编译过程中出现错误时的原因是因为编译器找不到mysql_init,mysql_close等的具体实现.虽然我们包括了正确的头文件,但是我们在编译的时候还是要连接确定的库.对于一些常用的函数的实现,gcc编译器会自动去连接一些常用库,这样我们就没有必要自己去指定了,如:printf函数.在本程序中要通过-L选项包含库文件的路径:
    gcc -o conn conn.c -L /usr/local/mysql/lib/*.a -lz

    或者:

    命令行后加入 -lmysqlclient
    例如对mysqlconn.c编译
    gcc mysqlconn.c -o conn -l mysqlclient
    编译即可成功
  • TA的每日心情
    慵懶
    2016-6-11 10:25
  • 簽到天數: 3 天

    [LV.2]偶爾看看I

    48

    主題

    102

    帖子

    729

    積分

    高級會員

    Rank: 4

    積分
    729

    活躍會員

    板凳
    發表於 2015-2-12 18:19:58 | 只看該作者 简体中文繁體中文
    您需要登錄後才可以回帖 登錄 | 立即註冊

    本版積分規則

    小黑屋|手機版|Archiver|Hippies 手作皮革工作坊  

    GMT+8, 2024-4-27 04:15 , Processed in 0.056653 second(s), 18 queries , Apc On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

    快速回復 返回頂部 返回列表