Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4249179
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: Mysql/postgreSQL

2011-08-10 09:53:57

1. 我们需要设置 mysql 登录名、及密码
 
  注意是 root权限
 1.1. 启动数据库
  #./bin/mysqld_safe --defaults-file=./my.cnf --user=ywx &
  如果这种方法启动设置不了, 这么启动
  #./bin/mysqld_safe --defaults-file=./my.cnf --user=root &

 1.2. 设置密码
  #./bin/mysqladmine --defaults-file=./my.cnf -u root password 123456
  这是直接设置密码

 1.3. 登陆 数据库
  #./bin/mysql --defaults-file=./my.cnf -u root -p

这样我们就设置好登陆密码 ,登陆名为 root


2.1 我们新建数据库test_c,增加一个表test_table

  1. mysql> create database test_c;

  2. mysql> create table test_table(id int not null primary key, name varchar(32) not null);

  3. mysql> insert into test_table values (100,'jack');
  4. Query OK, 1 row affected (0.00 sec)

  5. mysql> insert into test_table values (101,'mark');
  6. Query OK, 1 row affected (0.00 sec)

  7. mysql> insert into test_table values (102,'addrew');
  8. Query OK, 1 row affected (0.00 sec)

3. 编写 C 应用程序

   程序附件:
 c_mysql.rar  
  1. #include "/opt/mysql5151/include/mysql/mysql.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>

  4. int main(int argc, char *argv[])
  5. {
  6.     MYSQL *mysql;
  7.     MYSQL_RES *res;
  8.     MYSQL_ROW row;
  9.     char *query = "select * from test_table";//选择全部数据
  10.     int t, r;

  11.     mysql_init(mysql);
  12.     if(mysql == NULL) //假如初始化失败
  13.     {
  14.         printf("error init\n");
  15.     }

  16.     if(!mysql_real_connect(mysql,"localhost","root","123456","test_c",0,NULL,0))
  17.     {
  18.         printf("error connecting to database:%s\n",mysql_error(mysql));
  19.     }
  20.     else
  21.     {
  22.         printf("connected...\n");
  23.     }

  24.     //t = mysql_query(mysql,"set names utf8"); //设置 支持中文
  25.     t=mysql_query(mysql,query);
  26.     if(t)
  27.     {
  28.         printf("error making query :%s\n",mysql_error(mysql));
  29.     }
  30.     else
  31.     {
  32.         printf("query made ...\n");

  33.         res = mysql_use_result(mysql);
  34.         //res=mysql_store_result(mysql);
  35.         if(res)
  36.         {    
  37.             printf("mysql_use_result ok..\n");
  38.                     
  39.             for(r = 0; r <= mysql_field_count(mysql); r++)
  40.             {
  41.                 row = mysql_fetch_row(res);
  42.                 if(row < 0)
  43.                     break;
  44.                 for(t = 0; t < mysql_num_fields(res); t++)
  45.                 {
  46.                     printf("%s",row[t]);
  47.                 }
  48.                 printf("\n");
  49.             }
  50.                 
  51.         }
  52.         else
  53.         {
  54.             printf("error mysql_use_result\n");
  55.         }
  56.         mysql_free_result(res);
  57.     }
  58.     mysql_close(mysql);

  59. //当我用 return 0作为返回时, 出现了 段错误 使用exit 就没有错误了。以后都用 exit 退出程序
  60.     exit(EXIT_SUCCESS);
  61. }

  1. root@ywx:/home/ywx/yu/mysql-/c# todos -b c_mysql.c
  2. root@ywx:/home/ywx/yu/mysql-/c# ls
  3. c_mysql c_mysql.bak c_mysql.c
  4. root@ywx:/home/ywx/yu/mysql-/c# cp c_mysql.c /mnt/hgfs/share_linux/
  5. root@ywx:/home/ywx/yu/mysql-/c# cp c_mysql.bak c_mysql.c


4. 编译程序

  1. root@ywx:/home/ywx/yu/mysql-/c# gcc -I/opt/mysql5151/include/mysql c_mysql.c -L/opt/mysql5151/lib/mysql -lmysqlclient -o c_mysql

   -I 指定位置搜索头文件(include) 因为我们是敬爱功能 数据库安装在了/opt/mysql5151 下的
  
   -L 指定搜索链接库的位置
  1. root@ywx:/opt/mysql5151/lib/mysql# ls
  2. libdbug.a libmysqlclient_r.a libmysqlclient.so.16
  3. libheap.a libmysqlclient_r.la libmysqlclient.so.16.0.0
  4. libmyisam.a libmysqlclient_r.so libmystrings.a
  5. libmyisammrg.a libmysqlclient_r.so.16 libmysys.a
  6. libmysqlclient.a libmysqlclient_r.so.16.0.0 libvio.a
  7. libmysqlclient.la libmysqlclient.so plugin

   -lmysqlclient  表示需要mysqlclient 动态连接库,在L中,我们已经指定库文件中包含了 libmysqlclient.so.16 和 libmysqlclient.so.16.0.0  但是这样编译还是提示找不到 mysqlclient 动态库文件。。
  错误提示:
  1. root@ywx:/home/ywx/yu/mysql-/c# ./c_mysql
  2. ./c_mysql: error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or directory

   我这样解决:做个软链接  
  1. root@ywx:/opt/mysql5151/lib/mysql# ln -s libmysqlclient.so.16 /usr/lib/

  
  错误2 :


  /
/当我用 return 0作为返回时,出现了段错误 使用exit 就没有错误了。以后都用 exit 退出程   
exit(EXIT_SUCCESS);
    


5. 执行文件

  1. root@ywx:/home/ywx/yu/mysql-/c# ./c_mysql
  2. connected...
  3. query made ...
  4. mysql_use_result ok..
  5. 100jack
  6. 101mark
  7. 102addrew


好了,一个 C语言应用程序 访问数据库就好了。。



阅读(890) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~