How to interact with 2 databases in C using MySQL? -
i working on project have count runners. @ point, have transfer data local database (mysqllocal) 1 of high school i'm working in (mysqllycee).
i think doing idea, reason have segfault when executing program.
#include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netdb.h> #include <netinet/in.h> #include <unistd.h> #include <string.h> #include <mysql.h> int main(int argc, char *argv[]){ mysql mysqllocal; mysql_res *result = null; mysql_row row; char requete[150]; int num_champs; char noparticipant[5]; char kmparcourus[3]; mysql_init(&mysqllocal); if(!mysql_real_connect(&mysqllocal,"127.0.0.1","root","debianccf","localccf",0,null,0))printf("error on first connect"); sprintf(requete,"select no_participant, kmparcourus participants no_course = %s",argv[5]); if(!mysql_query(&mysqllocal,requete))printf("error on first query"); result = mysql_use_result(&mysqllocal); num_champs=mysql_num_fields(result); mysql_close(&mysqllocal); mysql mysqllycee; mysql_init(&mysqllycee); if(!mysql_real_connect(&mysqllycee,argv[1],argv[2],argv[3],argv[4],0,null,0))printf("error on second connect"); int i; while ((row = mysql_fetch_row(result))){ unsigned long *lengths; lengths = mysql_fetch_lengths(result); for(i=0;i<num_champs;i++){ if(i==0)sprintf(noparticipant,"%.*s", (int) lengths[i], row[i] ? row[i] : "null"); if(i==1)sprintf(kmparcourus,"%.*s", (int) lengths[i], row[i] ? row[i] : "null"); } sprintf(requete,"update participants set kmparcourus=%s no_participant=%s",kmparcourus,noparticipant); if(!mysql_query(&mysqllycee,requete))printf("error on second query"); } mysql_free_result(result); mysql_close(&mysqllycee); return 0; }
i'm working on debian 8, , compiling following command :
gcc updatelycee.c -o updatelycee -lmysqlclient -l/usr/lib64/mysql -i/usr/include/mysql;
edit: added mysql checks, still segfault when starting program.
you close connection local database, , later try fetch rows result set associated connection. not work.
if want transfer data 1 db other must either
first slurp wanted data 1 db memory (fetch rows , store contents need in ordinary arrays, instance), or
hold connections both databases open @ same time.
Comments
Post a Comment