Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - Mysql e C
Forum - C/C++ - Mysql e C

Avatar
edo42 (Member)
Pro


Messaggi: 102
Iscritto: 30/07/2009

Segnala al moderatore
Postato alle 14:08
Venerdì, 16/07/2010
Ciao a tutti!!
Mi sapete dire come si fa a connettersi a un database mysql remoto da C?
Devo eseguire un paio di query e con php ci riesco ma in C non ci sono i comandi, che cosa devo scaricare?

P.S. Io uso mac, ma preferirei un sistema multipiattaforma.

PM Quote
Avatar
nihal (Ex-Member)
Pro


Messaggi: 109
Iscritto: 12/04/2010

Segnala al moderatore
Postato alle 14:16
Venerdì, 16/07/2010
Quando scarichi e installi MySQL, con esso vengono fornite anche le API C. Ti basta includere l'header mysql.h e iniziare. Un buon punto di partenza è questo: http://dev.mysql.com/doc/refman/5.1/en/c.html. Ricordati di indicare al linker la libreria mysqlclient.

PM Quote
Avatar
edo42 (Member)
Pro


Messaggi: 102
Iscritto: 30/07/2009

Segnala al moderatore
Postato alle 18:55
Venerdì, 16/07/2010
Ma quale di quessti devo scaricare?

MySQL Community Server
MySQL Cluster
MySQL Workbench (GUI Tool)
MySQL Connectors

PM Quote
Avatar
nihal (Ex-Member)
Pro


Messaggi: 109
Iscritto: 12/04/2010

Segnala al moderatore
Postato alle 22:09
Venerdì, 16/07/2010
I connectors

PM Quote
Avatar
edo42 (Member)
Pro


Messaggi: 102
Iscritto: 30/07/2009

Segnala al moderatore
Postato alle 7:58
Sabato, 17/07/2010
Percui io ho scaricato il pacchetto:

Connector/C 6.0.2
Mac OS X 10.5 (x86, 32-bit), Compressed TAR Archive

Perchè io compilo con xcode l'applicazione in i386 e sono su 10.6 che è compatibile con 10.5

Quindi seleziono Linked Freamworks e gli dico existing freamworks e seleziono tutti i file della cartella lib.

Successivamente aggiungo le header al progetto e creo un semplice file chiamato mysql.c
Codice sorgente - presumibilmente C++

  1. #include "def.c"
  2. #include "mysql.h"
  3. #include "Md5.c"
  4.  
  5. #define MYSQL_ERROR        -1
  6. #define MYSQL_GOOD                      1
  7.  
  8. MYSQL *conn;
  9. MYSQL_RES *result;
  10. MYSQL_ROW row;
  11.  
  12. int check_user_and_pass(char *user, char *pass)
  13. {
  14.         char query[1000];
  15.         conn = mysql_init(NULL);
  16.         mysql_real_connect(conn, "-----------", "-----------", "-----------", "-----------",0, NULL, 0);
  17.         strcpy(query, "SELECT password FROM users WHERE username='");
  18.         strcat(query, user);
  19.         strcat(query, "'");
  20.         mysql_query(conn, query);
  21.         result = mysql_use_result(conn);
  22.         if(mysql_num_rows(result))
  23.         {
  24.                 row = mysql_fetch_row(result);
  25.                 if(strcmp(row[0], MDString(pass)) == 0)
  26.                 {
  27.                         return MYSQL_GOOD;
  28.                 }
  29.                 else
  30.                 {
  31.                         return MYSQL_ERROR;
  32.                 }
  33.         }
  34.         else
  35.         {
  36.                 return MYSQL_ERROR;
  37.         }
  38.         return MYSQL_GOOD;
  39. }



quindi compilo e nessun problema!!

Provo quindi a far partire il programma e nella console scrive:


[Session started at 2010-07-17 07:57:19 +0200.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1471) (Fri Jun 18 09:11:37 UTC 2010)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
sharedlibrary apply-load-rules all
Loading program into debugger…
warning: Unable to read symbols for "libmysql.16.dylib" (file not found).
warning: Unable to read symbols from "libmysql.16.dylib" (not yet mapped into memory).
Program loaded.
run
[Switching to process 391]
Running…
dyld: Library not loaded: libmysql.16.dylib
  Referenced from: /Users/Alberto/Documents/progetti/SDFChat/build/Debug/SDFChat.app/Contents/MacOS/SDFChat
  Reason: image not found
Data Formatters temporarily unavailable, will re-try after a 'continue'. (Not safe to call dlopen at this time.)

Come posso risolvere il problema?

PM Quote
Avatar
edo42 (Member)
Pro


Messaggi: 102
Iscritto: 30/07/2009

Segnala al moderatore
Postato alle 8:07
Sabato, 17/07/2010
Quello era un programma metà Obj-c e metà C.
Per provare ne ho fatto uno solo C; questo è quello che scrive:
[Session started at 2010-07-17 08:05:07 +0200.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1471) (Fri Jun 18 09:11:37 UTC 2010)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
Loading program into debugger…
sharedlibrary apply-load-rules all
warning: Unable to read symbols for "libmysql.16.dylib" (file not found).
warning: Unable to read symbols from "libmysql.16.dylib" (not yet mapped into memory).
Program loaded.
run
[Switching to process 570]
Running…
dyld: Library not loaded: libmysql.16.dylib
  Referenced from: /Users/Alberto/Documents/progetti/SDFChatServer/build/Debug/SDFChatServer
  Reason: image not found
No memory available to program now: unsafe to call malloc
Data Formatters temporarily unavailable, will re-try after a 'continue'. (The program being debugged was signaled while in a function called from GDB.
GDB remains in the frame where the signal was received.
To change this behavior use "set unwindonsignal on"
Evaluation of the expression containing the function (dlopen) will be abandoned.)
Previous frame inner to this frame (gdb could not unwind past this frame)
Previous frame inner to this frame (gdb could not unwind past this frame)
kill
quit

PM Quote
Avatar
edo42 (Member)
Pro


Messaggi: 102
Iscritto: 30/07/2009

Segnala al moderatore
Postato alle 9:10
Giovedì, 22/07/2010
Nessuno sa come risolvere il problema?

Grazie

PM Quote
Avatar
edo42 (Member)
Pro


Messaggi: 102
Iscritto: 30/07/2009

Segnala al moderatore
Postato alle 7:47
Venerdì, 23/07/2010
Ok, finalmente ho risolto il problema, ma non posso dirvi come sono riuscito perchè ho provato diverse cose e alla fine da solo si è messo ad andare. Non ho capito proprio come fare.

Grazie comunque

PM Quote