Cocoa tutorial – linking and using libmysqlclient in your application
Here’s a little tutorial on how to use libmysqlclient in a Cocoa application.
First of all you need to create a new Xcode project; suppose to create it in the directory: /path/to/your/project/.
Then you need to retrieve the libmysqlclient.a and libmysqlclient_r.a files located in the /usr/local/mysql/lib/ default directory. To do this you can, for example, start a new Terminal session and type the following commands:
cp /usr/local/mysql/lib/libmysqlclient.a /path/to/your/project/ cp /usr/local/mysql/lib/libmysqlclient_r.a /path/to/your/project/
Now go to Xcode and select “Project” -> “Edit Project Settings” in the main menu. Now select the “Build” tab and make the following changes to all your configurations (Debug and Release):
- “Architectures”: the same of your libmysqlclient architecture;
- “Valid Architectures”: architectures compatibles with your libmysqlclient architecture(s);
- “C/C++ Compiler Version”: GCC 4.2;
- uncheck “Prebinding”;
- turn on (if desired) the “Objective-C Grabage Collection”;
- “C Language Dialect”: C99 [-std=c99];
- check “Mismatched Return Type”;
- check “Unused Variables”.
Note that not all of these flags are strictly required for the correct use of libmysqclient.a and libmysqlclient_r.a; you can test them and see what happens when you change some of them.
Now you need to import the libraries. To do so, right click on the group “Frameworks” located in the “Groups & Files” tree in Xcode and select “Add” -> “Existing Frameworks…”. Locate the libmysqlclient.a file and click the Add button. Repeat the operation with the libmysqlclient_r.a file.
Now you need to tell Xcode to copy the two libraries in the bundle directory. To do so, you must create a new copy build phase in your target and put the libraries in it:
- Expand the “Targets” group in the “Groups & Files” tree;
- right click on your application and select: “Add” -> “New Build Phase” -> “New Copy Files Build Phase”;
- in the new window select “Frameworks” in the Destination popup menu;
- close the window;
- drag and drop the libmysqlclient.a and libmysqlclient_r.a files from the Frameworks group to the new “Copy Files” group.
Now you need to copy all the MySQL C API headers in your project directory. To do so you can, for example, do something like this:
cp -R /usr/local/mysql/include/ /path/to/your/project/mysql_include/
That’s all. To access any MySQL API function, you only need to import the mysql.h and unistd.h
Here’s a short example on how to use the API in a .m file:
... #include "mysql.h" #include "unistd.h" @implementation AppController ... -(void)connect { MYSQL conn; mysql_init(&conn); if(!mysql_real_connect(&conn, "127.0.0.1", "", "", "", 0, NULL, 0)) NSLog(@"%s", [NSString stringWithUTF8String:mysql_error(&conn)]); else NSLog(@"Connected"); } @end



Ciao,
volevo farti notare che i files libmysqlclient.a e libmysqlclient_r.a si trovano in /usr/local/mysql/lib/
comunque non ho ancora provato a seguirla ma appena fatto ti dico se mi funziona
ciao
aspetta, rettifico quello che ho detto
l’errore sta solo nel codice all’inizio dell’articolo.
scusami
Grazie per l’osservazione, corretto al volo
Ciao,
. ti volevo cheidere una cosa: la procedura che hai scritto riguarda applicazioni Mac o va bene anche per apps iPhone? io sto cercando in tutti i modi di far connettere la mia app a MySQL residente su Snow Leopard, ad esempio, solo che non riesco proprio. sai darmi qualche consiglio a proposito?
sono sempre io
La procedura che ho scritto è valida per l’inclusione di qualsiasi libreria in qualsiasi progetto di xCode. Il problema più che altro è che su iPhone girano solo applicazioni compilate per processori ARM e le librerie mysql che trovi sono solo per intel. Non credo neanche che sia possibile compilarle per ARM per colpa delle limitazioni di licenza imposte da Apple sui software di terze parti e quant’altro.. forse si riesce a far qualcosa, ma non ho mai provato e non ho idea di come compilare le librerie mysql
Potresti pensare di interfacciarti da iPhone ad un pannello web, il quale tramite php interroga il database mysql.. una sorta di phpmyadmin compatibile con iphone. A questo punto però diventerebbe un’applicazione poco interessante.. magari su internet trovi qualcosa a riguardo.
Ciao!
ti volevo dire che sono riuscito a connettermi a MySQL tramite questa procedura, in modo diretto. al momento la mia app la sto provando sul simulatore e sembra funzionare. non so dirti se invece funzioni su un cero iPhone perché non ne ho uno a portata di mano. quando avrò la possibilità di effettuare questa prova ti farò sapere. cmq grazie di tutto!
ciao Federico
Wow grande
complimenti
eheheheheheh grazie!!!