Linux C连接Mysql数据库

1. 连接数据库

[cpp]  view plain copy
  1. /*练习mysql数据库的连接*/  
  2. #include <stdio.h>  
  3. #include <mysql.h>  
  4.   
  5. int main()  
  6. {  
  7.     MYSQL mysql;  
  8.     int t, r;  
  9.     /*连接之前,先用mysql_init初始化MYSQL连接句柄*/  
  10.     mysql_init(&mysql);  
  11.     /*使用mysql_real_connect连接服务器,其参数依次为MYSQL句柄,服务器IP地址, 
  12.     登录mysql的用户名,密码,要连接的数据库等*/  
  13.     if(!mysql_real_connect(&mysql, "localhost""root""6748355""spytest", 0, NULL, 0)) {  
  14.         printf("Error connecting to Mysql!\n");  
  15.     }else {  
  16.         printf("Connected Mysql successful!\n");  
  17.     }  
  18.     /*关闭连接*/  
  19.     mysql_close(&mysql);  
  20.     return 0;  
  21. }  

2. 数据库查询

[cpp]  view plain copy
  1. /*练习mysql数据库的查询*/  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4. #include "mysql.h"  
  5.   
  6.   
  7. int main()  
  8. {  
  9.     MYSQL mysql;  
  10.     MYSQL_RES *res;  
  11.     MYSQL_ROW row;  
  12.     char *query;  
  13.     int flag, t;  
  14.     mysql_init(&mysql);  
  15.     if(!mysql_real_connect(&mysql, "localhost""root""6748355""spytest", 0, NULL, 0)) {  
  16.         printf("Failed to connect to Mysql!\n");  
  17.         return 0;  
  18.     }else {  
  19.         printf("Connected to Mysql successfully!\n");  
  20.     }  
  21.     query = "select * from log";  
  22.     /*查询,成功则返回0*/  
  23.     flag = mysql_real_query(&mysql, query, (unsigned int)strlen(query));  
  24.     if(flag) {  
  25.         printf("Query failed!\n");  
  26.         return 0;  
  27.     }else {  
  28.         printf("[%s] made...\n", query);  
  29.     }  
  30.   
  31.   
  32.     /*mysql_store_result讲全部的查询结果读取到客户端*/  
  33.     res = mysql_store_result(&mysql);  
  34.     /*mysql_fetch_row检索结果集的下一行*/  
  35.     while(row = mysql_fetch_row(res)) {  
  36.         /*mysql_num_fields返回结果集中的字段数目*/  
  37.         for(t=0; t<mysql_num_fields(res); t++)  
  38.         {  
  39.             printf("%s\t", row[t]);  
  40.         }  
  41.         printf("\n");  
  42.     }  
  43.     mysql_close(&mysql);  
  44.     return 0;  
  45. }  



3. 数据库插入数据

[cpp]  view plain copy
  1. /*练习mysql数据库的数据添加*/  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4. #include "mysql.h"  
  5.   
  6.   
  7. int main()  
  8. {  
  9.     MYSQL mysql;  
  10.     MYSQL_RES *res;  
  11.     MYSQL_ROW row;  
  12.     char *query;  
  13.     int flag, t;  
  14.     mysql_init(&mysql);  
  15.     if(!mysql_real_connect(&mysql, "localhost""root""6748355""spytest", 0, NULL, 0)) {  
  16.         printf("Failed to connect to Mysql!\n");  
  17.         return 0;  
  18.     }else {  
  19.         printf("Connected to Mysql successfully!\n");  
  20.     }  
  21.     query = "insert into log(title, keyword, content) values('sleep', 'rest', 'I have a dream')";  
  22.     /*插入,成功则返回0*/  
  23.     flag = mysql_real_query(&mysql, query, (unsigned int)strlen(query));  
  24.     if(flag) {  
  25.         printf("Insert data failure!\n");  
  26.         return 0;  
  27.     }else {  
  28.         printf("Insert data success!\n");  
  29.     }  
  30.   
  31.   
  32.     mysql_close(&mysql);  
  33.     return 0;  
  34. }  



4. 数据库删除

[cpp]  view plain copy
  1. /*练习mysql数据库的删除*/  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4. #include "mysql.h"  
  5.   
  6. int main()  
  7. {  
  8.     MYSQL mysql;  
  9.     MYSQL_RES *res;  
  10.     MYSQL_ROW row;  
  11.     char *query;  
  12.     int flag, t;  
  13.     mysql_init(&mysql);  
  14.     if(!mysql_real_connect(&mysql, "localhost""root""6748355""spytest", 0, NULL, 0)) {  
  15.         printf("Failed to connect to Mysql!\n");  
  16.         return 0;  
  17.     }else {  
  18.         printf("Connected to Mysql successfully!\n");  
  19.     }  
  20.     query = "delete from log where log_id=3";  
  21.     /*删除,成功则返回0*/  
  22.     flag = mysql_real_query(&mysql, query, (unsigned int)strlen(query));  
  23.     if(flag) {  
  24.         printf("Delete data failure!\n");  
  25.         return 0;  
  26.     }else {  
  27.         printf("Delete data success!\n");  
  28.     }  
  29.   
  30.     mysql_close(&mysql);  
  31.     return 0;  
  32. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值