C connection to MySQL database development: Xcode environment configuration and testing
I. development environment
Mac OS X 10.9.2 64-bit, Xcode5.1, and MySQL5.5.37 64-bit
MySQL installation directory:/usr/local/mysql
2. configure the compilation options for connecting xcode to mysql
1> add the mysql header file directory to the xcode header file search path.
Project properties --> Build Settings --> Search Paths --> Header Search Paths, add/Usr/local/mysql/include
2> add the mysql library file directory to the xcode library file search path
Project properties --> Build Settings --> Search Paths --> Library Search Paths, add/Usr/local/mysql/lib
3> add link tag options
Project properties --> Build Settings --> Linking --> Other Linker Flags, add the following tag:
-Lmysqlclient
-Lm
-Lz
4> link the mysql dynamic library to the/usr/lib Directory
Ln-s/usr/local/mysql/lib/libmysqlclient.18.dylib/usr/lib
III. test and development environment
//// Main. c // mysql database programming /// Created by YangXin on 14-5-22. // Copyright (c) 2014 yangxin. all rights reserved. // # include
# Include
# Include
# Include
MYSQL mysql; int main (int argc, const char * argv []) {/* before connection, use mysql_init to initialize the MYSQL connection handle */mysql_init (& mysql ); /* use mysql_real_connect to connect to the server. the parameters are MYSQL handle, server IP address, username and password used to log on to mysql, and the database to be connected. */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 ");}/* close connection */mysql_close (& mysql); return 0 ;}
Note:
If the dyld: Library not loaded: libmysqlclient.18.dylib error occurs, the dynamic Library of mysql is not linked to the/usr/lib directory.