您的位置:首页 > 数据库 > MySQL

ubuntu mysql c api的使用开发实例程序。

2011-11-16 14:37 781 查看
1.安装

apt-get install libmysqlclient-dev

2.mysql_config --libs

-Wl,-Bsymbolic-functions -rdynamic -L/usr/lib/mysql -lmysqlclient

3.mysql_config --cflags

-I/usr/include/mysql -DBIG_JOINS=1 -fno-strict-aliasing -DUNIV_LINUX -DUNIV_LINUX

http://www.cyberciti.biz/tips/linux-unix-connect-mysql-c-api-program.html

http://www.cyberciti.biz/files/mysql-c-api.c.txt

http://dev.mysql.com/doc/refman/5.0/en/c.html

http://pkgs.org/download/libmysqlclient.so.16()(64bit)

/* 
See url for more info: http://www.cyberciti.biz/tips/linux-unix-connect-mysql-c-api-program.html 
*/
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
   MYSQL *conn;
   MYSQL_RES *res;
   MYSQL_ROW row;
  /* Change me */
   char *server = "localhost";
   char *user = "root";
   char *password = "";
   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);
  
  return 0;
}


/LQmysql# gcc -o output-file $(mysql_config --cflags) mysql-c-api.c $(mysql_config --libs)

/LQmysql# ./output-file MySQL Tables in mysql database:

columns_priv

db

event

func

general_log

help_category

help_keyword

help_relation

help_topic

host

ndb_binlog_index

plugin

proc

procs_priv

servers

slow_log

tables_priv

time_zone

time_zone_leap_second

time_zone_name

time_zone_transition

time_zone_transition_type

user

警告: 隐式声明与内建函数 ‘exit’ 不兼容

把stdlib.h包含进来
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: