Oppure

Loading
16/07/10 12:08
edo42
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.
aaa
16/07/10 12:16
nihal
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: dev.mysql.com/doc/refman/5.1/en/…. Ricordati di indicare al linker la libreria mysqlclient.
aaa
16/07/10 16:55
edo42
Ma quale di quessti devo scaricare?

MySQL Community Server
MySQL Cluster
MySQL Workbench (GUI Tool)
MySQL Connectors
aaa
16/07/10 20:09
nihal
I connectors
aaa
17/07/10 5:58
edo42
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
#include "def.c"
#include "mysql.h"
#include "Md5.c"

#define MYSQL_ERROR        -1
#define MYSQL_GOOD			1

MYSQL *conn;
MYSQL_RES *result;
MYSQL_ROW row;

int check_user_and_pass(char *user, char *pass)
{
	char query[1000];
	conn = mysql_init(NULL);
	mysql_real_connect(conn, "-----------", "-----------", "-----------", "-----------",0, NULL, 0);
	strcpy(query, "SELECT password FROM users WHERE username='");
	strcat(query, user);
	strcat(query, "'");
	mysql_query(conn, query);
	result = mysql_use_result(conn);
	if(mysql_num_rows(result))
	{
		row = mysql_fetch_row(result);
		if(strcmp(row[0], MDString(pass)) == 0)
		{
			return MYSQL_GOOD;
		}
		else
		{
			return MYSQL_ERROR;
		}
	}
	else
	{
		return MYSQL_ERROR;
	}
	return MYSQL_GOOD;
}



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?
aaa
17/07/10 6:07
edo42
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
aaa
22/07/10 7:10
edo42
Nessuno sa come risolvere il problema?

Grazie
aaa
23/07/10 5:47
edo42
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
aaa