跳至內容
Sars' History
使用者工具
登入
網站工具
工具
顯示頁面
舊版
反向連結
最近更新
多媒體管理器
網站地圖
登入
最近更新
多媒體管理器
網站地圖
足跡:
mysql:c_api
本頁是唯讀的,您可以看到原始碼,但不能更動它。您如果覺得它不應被鎖上,請詢問管理員。
====== MySQL C API ====== http://dev.mysql.com/doc/refman/5.0/en/c.html http://dev.mysql.com/doc/refman/6.0/en/c.html ====== Connect MySQL server using C program API under Linux or UNIX ====== http://www.cyberciti.biz/tips/linux-unix-connect-mysql-c-api-program.html ===== 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. <code> /* 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); } </code> 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: <code> -L/usr/lib64/mysql -lmysqlclient -lz -lcrypt -lnsl -lm -L/usr/lib64 -lssl -lcrypto </code> 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: <code> -I/usr/include/mysql -g -pipe -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing </code> 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: <code> 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 </code> ===== References: ===== * MySQL C API - A must read - official MySQL C API documentation
mysql/c_api.txt
· 上一次變更: 2009/01/27 16:13 由
wenpei
頁面工具
顯示頁面
舊版
反向連結
回到頁頂