Xcode环境配置mysql
本文默认mysql安装目录为/usr/local/mysql
- 在项目的header search paths中添加/usr/local/mysql/includ
2.在项目的library search paths中添加/usr/local/mysql/li
3.在项目的 Other Linker Flags中分别添加 -lm -lz -lmysqlclient
4.c语言链接mysql代码如下:
if(!mysql_init(&conn)) { fprintf(stderr, "init mysql failed! no free memory!\n"); exit(0); }else{ fprintf(stdout, "init mysql successful!\n"); }//初始化数据库 if(!mysql_real_connect(&conn, "localhost", "root", "12345", "mydb", 3308, NULL, 0)) //分别使用主机名称,用户名,密码,数据库名称,端口 { fprintf(stderr, "mysql connect failed!: Error %u(%s)\n", mysql_errno(&conn), mysql_error(&conn)); mysql_close(&conn); exit(0); }else{ fprintf(stdout, "mysql connect successful!\n"); }//链接数据库 if(mysql_select_db(&conn, "mydb")) { fprintf(stderr, "use database failed!: Error %u(%s)\n", mysql_errno(&conn), mysql_error(&conn)); mysql_close(&conn); exit(0); }else{ fprintf(stdout, "use database succeessful!\n"); }//选择数据库 mysql_query(&conn, "SET NAMES UTF8");//防止乱码,mysql为UTF-8编码