A meghajtóprogram letöltéséhez látogassuk meg a következő oldalt:
Ha telepítő nélküli verziót választottuk 32-bites rendszerre (Windows (x86, 32-bit), ZIP), akkor egy ehhez hasonló állományt kapunk:
Csomagoljuk ki tetszőleges helyre. Például:
c:\mysql-connector-c-6.1.5-win32\
#include <mysql.h> main() { MYSQL *sql; MYSQL_RES *res; MYSQL_ROW row; if (!(mysql_real_connect(sql, "localhost", "root", "titok", "dbname", 0, NULL, 0))) { fprintf(stderr, "Nem tudok csatlakozni a MySQL adatbázishoz (%s)", mysql_error(sql)); exit(1); } if (mysql_query(sql,"insert into table (a) values (1)")!=0) fprintf(stderr, "Hiba! %s", mysql_error(sql)); mysql_close(sql); }
#include <stdio.h> #include <stdlib.h> #include <mysql.h> int main(int argc, char **argv) { printf("Kezdes\n"); MYSQL *conn; //~ MYSQL_RES *res; //~ MYSQL_ROW row; printf("%s\n", mysql_get_client_info()); conn = mysql_init(NULL); if (!(mysql_real_connect(conn, "192.168.5.5", "test", "titok", "test", 0, NULL, 0))) { fprintf(stderr, "Nem tudok csatlakozni a MySQL adatbázishoz (%s)", mysql_error(conn)); exit(1); } if (mysql_query(conn,"insert into Szemelyek (dolgozo) values ('Füles Bagoly')")!=0) fprintf(stderr, "Hiba! %s", mysql_error(conn)); mysql_close(conn); return 0; }
CC = c:/MinGW/bin/gcc RM = rm MYSQLINC = -Ic:/mysql/mysqlConnectorC/include MYSQLLIB = -Lc:/mysql/mysqlConnectorC/lib STDINC = -Ic:/MinGW/include STDLIB = -Lc:/MinGW/lib TARGET = program01.exe SRCS = program01.c OBJS = program01.o CFLAGS = LDFLAGS = -lmysql LIBS = $(STDLIB) $(MYSQLLIB) INCLUDES = $(STDINC) $(MYSQLINC) %.o : %.c $(CC) -c $(CFLAGS) $(INCLUDES) -o $@ $< $(TARGET): $(OBJS) $(CC) $(LDFLAGS) $(LIBS) -o $@ $^ clean: $(RM) -f $(TARGET) $(OBJS)
A libmysql.dll-t útvonalba kell helyezni. Nálam ez itt van:
c:\mysql\mysqlConnectorC\lib\
Settings -> Compiler ->
Global compiler settings ikon Search directories fül Compiler alfül Add gomb Keressük ki: C:\mysql-connector-c-6.1.5-win32\include
Global compiler settings ikon Search directories fül Linker alfül Add gomb Keressük ki: C:\mysql-connector-c-6.1.5-win32\lib
Global compiler settings ikon Other linker options: Linker settings: -lmysql
Global compiler settings ikon Linker libraries: Add gomb libmysql.dll kikeressük, hozzáadjuk
Teszteljük:
#include <stdio.h> #include <mysql.h> int main(){ printf("%s\n", mysql_get_client_info()); return 0; }
A Path környezeti változóba vegyük fel a következőt:
C:\mysql-connector-c-6.1.5-win32\lib
Eredmény ehhez hasonló kell legyen:
6.1.5
#include <stdio.h> #include <mysql.h> int main() { printf("%s\n", mysql_get_client_info()); printf("teszt\n"); return 0; }
TARGET = program01.exe CC = "c:\Program Files (x86)\CodeBlocks\MinGW\bin\gcc" CFLAGS = -Wall -g LDFLAGS = -lmysql SRCS = program01.c OBJS = program01.o MYSQLINCLUDE = -Ic:/mysql-connector-c-6.1.5-win32/include/ MYSQLLIB = -Lc:/mysql-connector-c-6.1.5-win32/lib/ STDINCLUDE = -I"c:/Program Files (x86)/CodeBlocks/MinGW/include/" STDLIB = -L"c:/Program Files (x86)/CodeBlocks/MinGW/lib/" INCLUDES = ${STDINCLUDE} ${MYSQLINCLUDE} LIBS = ${STDLIB} ${MYSQLLIB} ${TARGET}: ${OBJS} ${CC} ${LDFLAGS} ${LIBS} -o ${TARGET} ${OBJS} ${OBJS}: ${SRCS} ${CC} ${CFLAGS} ${INCLUDES} -c ${SRCS} clean: rm ${OBJS} ${TARGET}
apt-get install libmysqlclient-dev
#include <stdio.h> #include <stdlib.h> #include <mysql/mysql.h> int main(int argc, char **argv) { MYSQL *sql; if (!(mysql_real_connect(sql, "localhost", "root", "titok", "dbname", 0, NULL, 0))) { fprintf(stderr, "Nem tudok csatlakozni a MySQL adatbázishoz (%s)", mysql_error(sql)); exit(1); } if (mysql_query(sql,"insert into table (a) values (1)")!=0) fprintf(stderr, "Hiba! %s", mysql_error(sql)); mysql_close(sql); return 0; }
CC = gcc LDFLAGS = -lmysqlclient TARGET = program01 OBJECTS = program01.o SOURCES = program01.c HEADER = $(TARGET): $(OBJECTS) $(CC) $(LDFLAGS) -o $(TARGET) $(OBJECTS) $(OBJECTS): $(SOURCES) $(CC) -c -o $(OBJECTS) $(SOURCES) clean: rm -f $(TARGET) $(OBJECTS);
#include <stdio.h> #include <stdlib.h> #include <mysql/mysql.h> int main(int argc, char **argv) { MYSQL *con = mysql_init(NULL); if (!(mysql_real_connect(con, "localhost", "zoldUser", "titok", "zoldZrt", 0, NULL, 0))) { fprintf(stderr, "Nem tudok csatlakozni a MySQL adatbázishoz (%s)", mysql_error(con)); exit(1); } if (!mysql_set_character_set(con, "utf8")) { printf("A kliens új karakterkódolása: %s\n", mysql_character_set_name(con)); } if (mysql_query(con,"select nev, fizetes from szemelyek")!=0) fprintf(stderr, "Hiba! %s", mysql_error(con)); MYSQL_RES *result = mysql_store_result(con); if (result == NULL) { fprintf(stderr, "%s\n", mysql_error(con)); mysql_close(con); exit(1); } int num_fields = mysql_num_fields(result); MYSQL_ROW row; while ((row = mysql_fetch_row(result))) { int i; for(i = 0; i < num_fields; i++) { printf("%s ", row[i] ? row[i] : "NULL"); } printf("\n"); } mysql_free_result(result); mysql_close(con); return 0; }