Win 7下Dev-C++连接mysql配置

1.首先安装Dev-C++

2.配置编译器

工具->编译器选项->目录->二进制
E:\Program Files\Dev-Cpp\MinGW64\bin
E:\Program Files\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin

工具->编译器选项->目录->库
E:\Program Files\Dev-Cpp\MinGW64\lib32
E:\Program Files\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib32

工具->编译器选项->目录->C包含文件
E:\Program Files\Dev-Cpp\MinGW64\include
E:\Program Files\Dev-Cpp\MinGW64\x86_64-w64-mingw32\includ
E:\Program Files\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.8.1\include

工具->编译器选项->目录->C++包含文件
E:\Program Files\Dev-Cpp\MinGW64\include
E:\Program Files\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include
E:\Program Files\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.8.1\include
E:\Program Files\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++


3.安装libmysql
工具->检查更新->Select devpak server:devpaks.org Community Devpaks->Check for updates
选择libmysql
工具->Package Manager可以管理安装的dev文件

4. 添加libmysql.dll
把E:\Program Files\MySQL\mysql-5.6.19-win32\lib\libmysql.dll放到C:\Windows\System32

提示:文件编译成功后,修改编译器选项和项目属性参数无效,要删除项目目录下的.o后缀文件或者修改程序代码才会重新编译

5.添加-lmysql
undefined reference to `mysql_server_init@12'
添加-lmysql到 项目->项目选项->参数->Linker:

undefined reference to `mysql_library_init'
找到ref文件中的mysql_library_init@12或者其它类似的,去掉后缀,改为mysql_library_init再重新生成.a文件

使用reimp根据lib生成a文件

6.添加libmySQL.a和libmySQL.def目录
cannot find -lmysql
libmySQL.a和libmySQL.def在E:\Program Files\Dev-Cpp\lib目录下
添加E:\Program Files\Dev-Cpp\lib到 工具->编译器选项->目录->库
或者添加E:\Program Files\Dev-Cpp\lib到 项目->项目选项->文件/目录->库目录

7.添加mysql.h目录
mysql.h: No such file or directory
mysql.h在E:\Program Files\Dev-Cpp\include\MySQL目录下,如果安装了mysql数据库,也可以添加数据库目录下的mysql.h目录,E:\Program Files\MySQL\mysql-5.6.19-win32\include
添加E:\Program Files\Dev-Cpp\include\MySQL到 工具->编译器选项->目录->C包含文件
或者添加E:\Program Files\Dev-Cpp\include\MySQL到 项目->项目选项->文件/目录->包含文件目录

Win 7下Dev-C++连接mysql配置_第1张图片

Win 7下Dev-C++连接mysql配置_第2张图片

Win 7下Dev-C++连接mysql配置_第3张图片

#include 
#include 
#include 
#include 
#include 
static char* server_groups[] = { "embedded", "server", "this_program_server",
		(char*) NULL };
int main() {
	MYSQL mysql;
	MYSQL_RES *res;
	MYSQL_ROW row;
	char sqlcmd[200];
	int unsigned t;
	mysql_library_init(0, NULL, server_groups);//初始化MYSQL数据库
	mysql_init(&mysql);//初始化MYSQL标识符,用于连接
	mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "gbk");//设置编码
	if (!mysql_real_connect(&mysql, "localhost", "root", "111111", "mysql",
			0, NULL, 0)) {
		fprintf(stderr, "无法连接到数据库,错误原因是:%s/n", mysql_error(&mysql));
	} else {
		puts("数据库连接成功");
		sprintf(sqlcmd, "%s", "select * from ME_MENU");
		t = mysql_real_query(&mysql, sqlcmd, (unsigned int) strlen(sqlcmd));
		if (t) {
			printf("查询数据库失败%s/n", mysql_error(&mysql));
		} else {
			res = mysql_store_result(&mysql);//返回查询的全部结果集
			while ((row = mysql_fetch_row(res))) {//mysql_fetch_row取结果集的下一行
				for (t = 0; t < mysql_num_fields(res); t++) {//结果集的列的数量
					printf("%s\t", row[t]);
				}
				printf("\n");
			}
			mysql_free_result(res);//操作完毕,查询结果集
		}
		mysql_close(&mysql);//关闭数据库连接
	}
	mysql_library_end();//关闭MySQL库
	return EXIT_SUCCESS;
}


你可能感兴趣的:(c,mysql)