Home  >  Article  >  Database  >  C连接MySQL数据库开发之Xcode环境配置及测试_MySQL

C连接MySQL数据库开发之Xcode环境配置及测试_MySQL

WBOY
WBOYOriginal
2016-06-01 13:13:151081browse

一、开发环境

Mac OS X 10.9.2 64位、Xcode5.1、MySQL5.5.37 64位

MySQL安装目录为:/usr/local/mysql

二、配置xcode连接mysql的编译选项

1> 将mysql头文件目录添加到xcode头文件搜索路径中

项目属性--> Build Settings --> Search Paths --> Header Search Paths,添加/usr/local/mysql/include


2> 将mysql库文件目录添加到xcode库文件搜索路径中

项目属性--> Build Settings --> Search Paths --> Library Search Paths,添加/usr/local/mysql/lib


3> 添加链接标记选项

项目属性--> Build Settings --> Linking --> Other Linker Flags,添加如下标记:

-lmysqlclient

-lm

-lz


4> 将mysql的动态库链接到/usr/lib目录下

ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib


三、测试开发环境

////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 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");	}		/*关闭连接*/	mysql_close(&mysql);	return 0;}</mysql.h></string.h></stdlib.h></stdio.h>
注意:

如果出现 dyld: Library not loaded: libmysqlclient.18.dylib错误,表示没有将mysql的动态库链接到/usr/lib目录下。

Statement:
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