Home > Database > Mysql Tutorial > C连接MySQL数据库开发之Linux环境完整示例演示(增、删、改、查_MySQL

C连接MySQL数据库开发之Linux环境完整示例演示(增、删、改、查_MySQL

WBOY
Release: 2016-06-01 13:13:17
Original
1085 people have browsed it

一、开发环境

ReadHat6.332位、mysql5.6.15、gcc4.4.6

二、编译

gcc-I/usr/include/mysql-L/usr/lib-lmysqlclient main.c -o main.out

-I:指定mysql头文件所在目录(默认去/usr/include目录下寻找所用到的头文件)

-L:指定mysql动态库文件所在目录(默认从/usr/lib目录查找)

-l:链接libmysqlclient.so动态库

-o:生成的可执行文件名

三、完整示例

////main.c//mysql数据库编程////Created by YangXin on 14-5-22.//Copyright (c) 2014年 yangxin. All rights reserved.//#include <stdio.h>#include <stdlib.h>#include <string.h>#include <mysql.h>MYSQL mysql;// 查询int query();// 修改int update();// 添加数据my_ulonglong add();// 参数化添加数据my_ulonglong addByParams();// 删除数据my_ulonglong delete();// 打印数据库服务器信息void printMySqlInfo();int main(int argc, const char * argv[]){	/*连接之前,先用mysql_init初始化MYSQL连接句柄*/	mysql_init(&mysql);		/*使用mysql_real_connect连接服务器,其参数依次为MYSQL句柄,服务器IP地址,	 登录mysql的用户名,密码,要连接的数据库等*/	if(!mysql_real_connect(&mysql, "localhost", "root", "yangxin", "test", 0, NULL, 0)) {		printf("connecting to Mysql error:%d from %s/n",mysql_errno(&mysql), mysql_error(&mysql));		return -1;	}else {		printf("Connected Mysql successful!/n");	}		printMySqlInfo();		// 设置编码	mysql_query(&mysql, "set names utf8");		// 参数化添加数据	addByParams();		// 查询	query();		// 修改	update();		// 添加	add();		// 删除	delete();		/*关闭连接*/	mysql_close(&mysql);	return 0;}// 查询int query(){	int flag, i;	const char *sql = NULL;	MYSQL_RES *res = NULL;	MYSQL_ROW row = NULL;	MYSQL_FIELD *fields = NULL;	sql = "select * from t_user" ;	flag = mysql_real_query(&mysql, sql, (unsigned int)strlen(sql));	if (flag) {		printf("query error:%d from %s/n",mysql_errno(&mysql),mysql_error(&mysql));		return -1;	}		 // 将查询结果读取到内存当中,如果数据很多的情况会比较耗内存	res = mysql_store_result(&mysql);	// res = mysql_use_result(&mysql); // 需要用到的时候,每次从服务器中读取一行		// 字段数量	unsigned int field_count = mysql_field_count(&mysql);	printf("field_cout:%d/n",field_count);		// 查询总数	my_ulonglong rows = mysql_num_rows(res);	printf("%lld/n",rows);		// 获取所有字段	fields = mysql_fetch_fields(res);	for (int i = 0; i  10";	flag = mysql_real_query(&mysql, sql, strlen(sql));	if (flag) {		printf("delete data error:%d from %s/n",mysql_errno(&mysql), mysql_error(&mysql));		return -1;	}		my_ulonglong affected_rows = mysql_affected_rows(&mysql);	printf("删除的行数:%lld/n",affected_rows);		printf("success delete %lld record data !/n",affected_rows);	return affected_rows;}void printMySqlInfo(){	const char *stat = mysql_stat(&mysql);	const char *server_info = mysql_get_server_info(&mysql);	const char *clientInfo = mysql_get_client_info();	unsigned long version =	mysql_get_client_version();	const char *hostinfo =	mysql_get_host_info(&mysql);	unsigned long serverversion = mysql_get_server_version(&mysql);	unsigned int protoinfo = mysql_get_proto_info(&mysql);		printf("stat:%s/n",stat);	printf("server_info:%s/n",server_info);	printf("clientInfo:%s/n",clientInfo);	printf("version:%ld/n",version);	printf("hostinfo:%s/n",hostinfo);	printf("serverversion:%ld/n",serverversion);	printf("protoinfo:%d/n",protoinfo);		const char *charactername = mysql_character_set_name(&mysql);	printf("client character set:%s/n",charactername);	if (!mysql_set_character_set(&mysql, "utf8"))	{		printf("New client character set: %s/n",			 mysql_character_set_name(&mysql));	}}</mysql.h></string.h></stdlib.h></stdio.h>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template