首先开发Mac的应用程序之前请先安装Mysql服务
1. 安装MySql服务Mac版
下载 Download MySQL Community Server 并安装 下载地址:http://dev.mysql.com/downloads/mysql/
2.配置XCode项目
打开你的开发项目配置TARGETS的三个地方
1): Header Search Paths 加上两项 /usr/local/mysql/include , $(inherited)
2): Library Search Paths 加上两项 /usr/local/mysql/lib , $(inherited)
3): Other Linker Flags加上四项 -lmysqlclient , -lm , -lz , $(inherited)
如图:
Header Search Paths:
Library Search Paths
Other Linker Flags
3. 开发MySql应用
主要介绍Objective-C中MySql数据库的连接,查询等。
#import <mysql.h>
MYSQL mysql;
int mysql_connection(){
mysql_init(&mysql);
if (!mysql_real_connect(&mysql, "127.0.0.1", "root", "", "test", 3306, NULL, 0)){
NSLog(@"连接失败");
} else {
NSLog(@"连接成功");
}
return(0);
}
int mysql_execute(){
int i = mysql_query(&mysql, "SELECT * FROM testsss");
if (i) {
NSLog(@"查询失败");
}else{
NSLog(@"查询成功");
}
MYSQL_RES *result = mysql_store_result(&mysql);
if (result) {
int num_fields = mysql_num_fields(result);
int num_rows = mysql_num_rows(result);
NSLog(@"有多少条记录:%d",num_rows);
}
return 0;
}
如果编译报错 MySQL on OSX: Library not loaded: libmysqlclient.18.dylib 则执行以下代码:
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib