/* Simple C program that connects to MySQL Database server LIBS=`mysql_config --libs`; gcc ${LIBS} -Wall -o testmysql2 testmysql2.c Run with ./testmysql2 --chip or ./testmysql2 --tray */ #include #include #include int main(int argc, char **argv) { int doChip = 0; int doTray = 0; if (argc > 1) { if (strstr(argv[1],"chip")) { doChip=1; printf("get chip info \n"); } else if (strstr(argv[1],"tray")) { printf("get tray info \n"); doTray=1; } } if (!doChip && !doTray) { printf("select either chip or tray info \n"); printf("%s --chip\n", argv[0]); printf("%s --tray\n", argv[0]); return 0; } MYSQL *conn; MYSQL_RES *res; MYSQL_ROW row; int i; int num_fields; char *server = "renrum.astro.lu.se"; char *user = "backup"; // User can only read database //set the password for mysql server here char *password = "cle034a"; char *database = "sampa"; printf("mysql_init\n"); conn = mysql_init(NULL); /* Connect to database */ printf("Connect to database %s as user %s\n", server, user); if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) { printf("mysql connection failed\n"); fprintf(stderr, "%s\n", mysql_error(conn)); return(1); } /* send SQL query */ if (mysql_query(conn, "show tables")) { fprintf(stderr, "%s\n", mysql_error(conn)); return(1); } res = mysql_use_result(conn); /* output table name */ printf("MySQL Tables in mysql database:\n"); while ((row = mysql_fetch_row(res)) != NULL) printf("%s \n", row[0]); mysql_free_result(res); if (doChip) { //DS if (mysql_query(conn, "SELECT * FROM chip WHERE testnb=0 AND qrcode LIKE 'SAMPA%'")) { if (mysql_query(conn, "SELECT * FROM chip WHERE testnb=0")) { fprintf(stderr, "%s\n", mysql_error(conn)); return(1); } res = mysql_use_result(conn); num_fields = mysql_num_fields(res); puts("CHIP"); puts("===="); while ((row = mysql_fetch_row(res)) != NULL) { for(i = 0; i < num_fields; i++) { printf("%s ", row[i] ? row[i] : "NULL"); } printf("\n"); } mysql_free_result(res); } if (doTray) { if (mysql_query(conn, "SELECT * FROM trays")) { fprintf(stderr, "%s\n", mysql_error(conn)); return(1); } res = mysql_use_result(conn); num_fields = mysql_num_fields(res); puts("TRAYS"); puts("====="); while ((row = mysql_fetch_row(res)) != NULL) { for(i = 0; i < num_fields; i++) { printf("%s ", row[i] ? row[i] : "NULL"); } printf("\n"); } mysql_free_result(res); } /* close connection */ mysql_close(conn); return 0; }