Linux c访问mysql 编写入门

 

(一) 前置条件:

(1)       Linux 已经安装好 mysql 数据库;

(2)       Linux 已经安装了 gcc 编译器;

 

(二)数据库准备:

为了便于描述,假设数据库的 root 用户密码为 root_pwd 。

 

(1)       以 root 用户登陆数据库

#mysql -uroot –proot_pwd

mysql>

 

(2)       创建数据 testdb

mysql> create database testdb;

 

(3)       创建表

mysql> use testdb;

mysql> create table t_users(userid int not null, username varchar(20) not null);

 

(4)       向表中插入一条记录

mysql> insert into t_users(userid, username) values(1, 'HuDennis');

 

(5)       创建用户

mysql> create user testdb_user;

 

(6)       对用户授权

mysql> grant select, insert, update, delete on *.* to 'testdb_user'@'localhost' identified by '123456';

mysql> flush privileges;

 

(7)       查看用户及该用户的权限

mysql> use mysql;

mysql> select user from user;

mysql> show grants for testdb_user@localhost;

 

(8)       [ 备用 ] 删除数据库及用户

mysql> drop database testdb;

mysql> show databases;

 

mysql> drop user testdb_user;

mysql> drop user testdb_user@localhost;

 

 

(三) C 源代码 testdb.c 准备:

  1. #include <mysql.h>   
  2. #include <stdlib.h>   
  3. #include <stdio.h>   
  4.    
  5. static char *server_options[] = { "mysql_test", "--defaults-file=my.cnf" };  
  6. int num_elements = sizeof(server_options)/ sizeof(char *);  
  7.    
  8. static char *server_groups[] = { "libmysqld_server", "libmysqld_client" };  
  9.    
  10. int main(void)  
  11. {  
  12.     if (mysql_server_init(num_elements, server_options, server_groups))  
  13.     {  
  14.             exit(1);  
  15.     }  
  16.    
  17.     MYSQL *conn;  
  18.     MYSQL_RES *res;  
  19.     MYSQL_ROW row;  
  20.     char *server = "localhost";  
  21.     char *user = "testdb_user";  
  22.     char *password = "123456";  
  23.     char *database = "testdb";  
  24.     conn = mysql_init(NULL);  
  25.    
  26.     /* Connect to database */  
  27.     if (!mysql_real_connect(conn, server,  
  28.                             user, password, database, 0, NULL, 0))  
  29.     {  
  30.         fprintf(stderr, "%s/n", mysql_error(conn));  
  31.         exit(1);  
  32.     }  
  33.    
  34.       /* send SQL query */  
  35.     if (mysql_query(conn, "select userid, username from t_users"))  
  36.     {  
  37.          fprintf(stderr, "%s/n", mysql_error(conn));  
  38.         exit(1);  
  39.     }  
  40.    
  41.     if ((res = mysql_use_result(conn)) == NULL)  
  42.     {  
  43.           fprintf(stderr, "%s/n", mysql_error(conn));  
  44.           exit(1);  
  45.     }  
  46.    
  47.     int num = mysql_num_fields(res);  
  48. while ((row = mysql_fetch_row(res)) != NULL)  
  49.     {  
  50.         int i=0;  
  51.         for (; i<num; i++)  
  52.         {  
  53.                 printf("%s ", row[i]);  
  54.         }  
  55.    
  56.         printf("/n");  
  57.     }  
  58.    
  59.     /* close connection */  
  60.     mysql_free_result(res);  
  61.     mysql_close(conn);  
  62.     /* Use any MySQL API functions here */  
  63.     mysql_server_end();  
  64.     return EXIT_SUCCESS;  
  65. }   

                           

(四) C 程序编译与运行:

(1) 编译 testdb.c

# gcc testdb.c -I/usr/local/mysql/include -L/usr/local/mysql/lib -lmysqlclient

注意: -I 和 -L 的路径应该根据 mysql 的安装路径不同而应该有所不同。同理,下面的 LD_LIBRARY_PATH 变量一样。

 

(2) 将库文件路径加入到 LD_LIBRARY_PATH 环境变量中去

# LD_LIBRARY_PATH =$LD_LIBRARY_PATH:/usr/local/mysql/lib

 

(3) 运行编译得到的可执行文件 a.out

#./a.out

1 HuDennis

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值