/* Simple C program that connects to MySQL Database server LIBS=`mysql_config --libs`; gcc ${LIBS} -Wall -o testmysqltray.exe testmysqltray.c Run with ./testmysqltray.exe 20 to get the info for tray 20 */ #include #include #include #include int main(int argc, char **argv) { int selTray = 1; if (argc > 1) { selTray = atoi(argv[1]); printf("get info for tray %d\n", selTray); } else { printf("select which tray you would like to get the info for, e.g. for tray 10 \n"); printf("%s 10\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); char sqlcmd[200]; if (selTray>0 && selTray<1000) { sprintf(sqlcmd, "SELECT * FROM trays where traynb=%d", selTray); if (mysql_query(conn, sqlcmd)) { fprintf(stderr, "%s\n", mysql_error(conn)); return(1); } res = mysql_use_result(conn); num_fields = mysql_num_fields(res); 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; }