comparison clock.c @ 11:176ee28e7464

switched from mysql to sqlite; (+ some cleanups)
author meillo@marmaro.de
date Wed, 23 Jul 2008 11:41:38 +0200
parents 8369454d4ec9
children 8db6497d6065
comparison
equal deleted inserted replaced
10:13c6828bd4a5 11:176ee28e7464
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <stdlib.h> 2 #include <stdlib.h>
3 #include <mysql.h>
4 3
5 #include "db.h" 4 #include "db.h"
6 #include "game.h" 5 #include "game.h"
7 6
8 7
9 8
10 void inc_time() { 9 void inc_time() {
11
12 /* get current time */ 10 /* get current time */
13 sprintf(query, "select time from game where name = '%s' ", gamename); 11 sprintf(query, "select time from game;");
14 db_query(query); 12 db_query(query);
15 result = mysql_store_result(conn); 13 if (sqlite3_step(stmt) == SQLITE_ROW) {
16 if (mysql_num_rows(result)) { 14 gametime = sqlite3_column_int(stmt, 0);
17 row = mysql_fetch_row(result); 15 printf("gametime: %d\n", gametime);
18 gametime = atoi(row[0]); 16 } else {
17 fprintf(stderr, "error: %s\n", sqlite3_errmsg(db));
19 } 18 }
20 mysql_free_result(result); 19 sqlite3_finalize(stmt);
21
22 20
23 /* increment time */ 21 /* increment time */
24 sprintf(query, " update game set time = '%d' where name = '%s' ", ++gametime, gamename); 22 sprintf(query, " update game set time = '%d';", ++gametime);
25 db_query(query); 23 db_query(query);
26 if (mysql_affected_rows(conn) > 0) { 24 if (sqlite3_step(stmt) == SQLITE_DONE) {
27 printf("time update successful\n"); 25 printf("time update successful\n");
28 printf("virtual time: %d\n", gametime); 26 printf("virtual time: %d\n", gametime);
29 } else { 27 } else {
30 printf("E: time update failed\n"); 28 printf("error: time update failed: %s\n", sqlite3_errmsg(db));
31 } 29 }
32 30 sqlite3_finalize(stmt);
33 } 31 }
34 32
35 33
36 int main(int argc, char* argv[]) { 34 int main(int argc, char* argv[]) {
37 printf(" --> clock\n"); 35 printf(" --> clock\n");
39 /* init */ 37 /* init */
40 if (argc != 2) { 38 if (argc != 2) {
41 printf("usage: %s <game>\n", argv[0]); 39 printf("usage: %s <game>\n", argv[0]);
42 exit(1); 40 exit(1);
43 } 41 }
44 gamename = argv[1]; 42 database = argv[1];
45 43
46 db_connect(); 44 db_connect();
47 check_game(); 45 read_time();
48 46
49 inc_time(); 47 inc_time();
50 48
51 /* 49 /*
52 while ((row = mysql_fetch_row(result)) != NULL) { 50 while ((row = mysql_fetch_row(result)) != NULL) {