C语言MySQL 基础操作(添加、查询和删除)分析

符号 阅读:284 2021-03-31 21:45:54 评论:0

MySQL 之数据添加

/*练习mysql数据库的查询*/ 
#include <stdio.h> 
#include <string.h> 
#include "/usr/local/mysql/include/mysql.h" 
 
 
int main() 
{ 
	MYSQL mysql; 
	MYSQL_RES *res; 
	MYSQL_ROW row; 
	char *query; 
	int flag, t; 
	mysql_init(&mysql); 
	if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "test", 0, NULL, 0)) { 
		printf("Failed to connect to Mysql!\n"); 
		return 0; 
	}else { 
		printf("Connected to Mysql successfully!\n"); 
	} 
	query = "select * from student"; 
	/*查询,成功则返回0*/ 
	flag = mysql_real_query(&mysql, query, (unsigned int)strlen(query)); 
	if(flag) { 
		printf("Query failed!\n"); 
		return 0; 
	}else { 
		printf("[%s] made...\n", query); 
	} 
 
 
	/*mysql_store_result讲全部的查询结果读取到客户端*/ 
	res = mysql_store_result(&mysql); 
	/*mysql_fetch_row检索结果集的下一行*/ 
	while(row = mysql_fetch_row(res)) { 
		/*mysql_num_fields返回结果集中的字段数目*/ 
		for(t=0; t<mysql_num_fields(res); t++) 
		{ 
			printf("%s\t", row[t]); 
		} 
		printf("\n"); 
	} 
	mysql_close(&mysql); 
	return 0; 
} 

编译:gcc -o mysqlselect -I /usr/local/include/mysql/ -L /usr/local/mysql/lib -lmysqlclient mysqlselect.c

执行:./mysqlselect

结果展示:

[[email protected] c]# gcc -o mysqlselect -I /usr/local/include/mysql/ -L /usr/local/mysql/lib -lmysqlclient mysqlselect.c 
[[email protected] c]# ls -a 
.   a.out       hello.c      mysqlselect.c  testmysql.c 
..  datatype.c  mysqlselect  testmysql      testmysql.o 
[[email protected] c]# ./mysqlselect 
Connected to Mysql successfully! 
[select * from student] made... 
1	1	 
2	2	 
3	3	 



MySQL 之插入

/*练习mysql数据库的数据添加*/   
#include <stdio.h>   
#include <string.h>   
#include "/usr/local/mysql/include/mysql.h"   
   
   
int main()   
{   
    MYSQL mysql;   
    MYSQL_RES *res;   
    MYSQL_ROW row;   
    char *query;   
    int flag, t;   
    mysql_init(&mysql);   
    if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "test", 0, NULL, 0)) {   
        printf("Failed to connect to Mysql!\n");   
        return 0;   
    }else {   
        printf("Connected to Mysql successfully!\n");   
    }   
    query = "insert into student(id,name) values(4,'5')";   
    /*插入,成功则返回0*/   
    flag = mysql_real_query(&mysql, query, (unsigned int)strlen(query));   
    if(flag) {   
        printf("Insert data failure!\n");   
        return 0;   
    }else {   
        printf("Insert data success!\n");   
    }   
   
   
    mysql_close(&mysql);   
    return 0;   
}   

编译:gcc -o mysqlinsert -I /usr/local/include/mysql/ -L /usr/local/mysql/lib -lmysqlclient mysqlinsert.c

执行:./mysqlinsert

结果展示:

[[email protected] c]# gcc -o mysqlinsert -I /usr/local/include/mysql/ -L /usr/local/mysql/lib -lmysqlclient mysqlinsert.c 
[[email protected] c]# ls -a 
.   a.out       hello.c      mysqlinsert.c  mysqlselect.c  testmysql.c 
..  datatype.c  mysqlinsert  mysqlselect    testmysql      testmysql.o 
[[email protected] c]# ./mysqlinsert 
Connected to Mysql successfully! 
Insert data success! 


MySQL 之删除

/*练习mysql数据库的删除*/   
#include <stdio.h>   
#include <string.h>   
#include "/usr/local/mysql/include/mysql.h"   
   
int main()   
{   
    MYSQL mysql;   
    MYSQL_RES *res;   
    MYSQL_ROW row;   
    char *query;   
    int flag, t;   
    mysql_init(&mysql);   
    if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "test", 0, NULL, 0)) {   
        printf("Failed to connect to Mysql!\n");   
        return 0;   
    }else {   
        printf("Connected to Mysql successfully!\n");   
    }   
    query = "delete from student  where log_id=1";   
    /*删除,成功则返回0*/   
    flag = mysql_real_query(&mysql, query, (unsigned int)strlen(query));   
    if(flag) {   
        printf("Delete data failure!\n");   
        return 0;   
    }else {   
        printf("Delete data success!\n");   
    }   
   
    mysql_close(&mysql);   
    return 0;   
}   


编译:gcc -o mysqldelete -I /usr/local/include/mysql/ -L /usr/local/mysql/lib -lmysqlclient mysqldelete.c

执行:./mysqldelete

执行结果:

[[email protected] c]# gcc -o mysqldelete -I /usr/local/include/mysql/ -L /usr/local/mysql/lib -lmysqlclient mysqldelete.c 
[[email protected] c]# ./mysqldelete 
Connected to Mysql successfully! 
Delete data success! 




声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号