garten

annotate clock.c @ 0:831599184108

inital commit can increment the time in the database
author meillo@marmaro.de
date Mon, 12 May 2008 21:24:20 +0200
parents
children 8369454d4ec9
rev   line source
meillo@0 1 #include <stdio.h>
meillo@0 2 #include <stdlib.h>
meillo@0 3 #include <mysql.h>
meillo@0 4
meillo@0 5 #include "db.h"
meillo@0 6
meillo@0 7 char query[512];
meillo@0 8
meillo@0 9
meillo@0 10 int check_game(char* gamename) {
meillo@0 11 int rows;
meillo@0 12
meillo@0 13 sprintf(query, "select id from simulation where name = '%s' ", gamename);
meillo@0 14 db_query(query);
meillo@0 15 result = mysql_store_result(conn);
meillo@0 16 rows = mysql_num_rows(result);
meillo@0 17 mysql_free_result(result);
meillo@0 18 return rows;
meillo@0 19 }
meillo@0 20
meillo@0 21
meillo@0 22 void inc_time(char* gamename) {
meillo@0 23 int time = 0;
meillo@0 24
meillo@0 25 /* get current time */
meillo@0 26 sprintf(query, "select time from simulation where name = '%s' ", gamename);
meillo@0 27 db_query(query);
meillo@0 28 result = mysql_store_result(conn);
meillo@0 29 if (mysql_num_rows(result)) {
meillo@0 30 row = mysql_fetch_row(result);
meillo@0 31 time = atoi(row[0]);
meillo@0 32 }
meillo@0 33 mysql_free_result(result);
meillo@0 34
meillo@0 35
meillo@0 36 /* increment time */
meillo@0 37 sprintf(query, " update simulation set time = '%d' where name = '%s' ", ++time, gamename);
meillo@0 38 db_query(query);
meillo@0 39 if (mysql_affected_rows(conn) > 0) {
meillo@0 40 printf("time update successful\n");
meillo@0 41 printf("simulation time: %d\n", time);
meillo@0 42 } else {
meillo@0 43 printf("E: time update failed\n");
meillo@0 44 }
meillo@0 45
meillo@0 46 }
meillo@0 47
meillo@0 48
meillo@0 49 int main(int argc, char* argv[]) {
meillo@0 50 int i;
meillo@0 51 char* gamename;
meillo@0 52
meillo@0 53 /* init */
meillo@0 54 if (argc != 2) {
meillo@0 55 printf("usage: %s <game>\n", argv[0]);
meillo@0 56 exit(1);
meillo@0 57 }
meillo@0 58 gamename = argv[1];
meillo@0 59
meillo@0 60 printf("gamename: %s\n", gamename);
meillo@0 61
meillo@0 62 db_connect();
meillo@0 63
meillo@0 64 if (check_game(gamename) != 1) {
meillo@0 65 printf("game '%s' does not exist\n", gamename);
meillo@0 66 exit(1);
meillo@0 67 }
meillo@0 68
meillo@0 69 inc_time(gamename);
meillo@0 70
meillo@0 71 /*
meillo@0 72 while ((row = mysql_fetch_row(result)) != NULL) {
meillo@0 73 for (i = 0; i < mysql_num_fields(result); i++) {
meillo@0 74 printf("%10s ", row[i]);
meillo@0 75 }
meillo@0 76 printf("\n");
meillo@0 77 }
meillo@0 78 */
meillo@0 79
meillo@0 80
meillo@0 81
meillo@0 82
meillo@0 83 db_close();
meillo@0 84
meillo@0 85 printf("the garten program\n");
meillo@0 86 return 0;
meillo@0 87 }