The default MySQL installation directory for this article is/usr/local/mysql
- Add/usr/local/mysql/includ to the project's header search paths
2. Add/usr/local/mysql/li to the project's Library search paths
3. Add-lm-lz-lmysqlclient in the other Linker flags of the project
The 4.c language link MySQL code is as follows:
if(!mysql_init (&conn)) {fprintf (stderr,"init mysql failed! no free memory!\n"); Exit (0); }Else{fprintf (stdout,"init MySQL successful!\n"); }//Initializing the database if(!mysql_real_connect (&conn,"localhost","Root","12345","MyDB",3308Null0)) //use host name, username, password, database name, port, respectively{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"); }//Link Database 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"); }//Select Databasemysql_query (&conn,"SET NAMES UTF8");//prevent garbled, MySQL for UTF-8 encoding
Xcode environment Configuration MySQL