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