diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clock.c	Mon May 12 21:24:20 2008 +0200
@@ -0,0 +1,87 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <mysql.h>
+
+#include "db.h"
+
+char query[512];
+
+
+int check_game(char* gamename) {
+	int rows;
+
+	sprintf(query, "select id from simulation where name = '%s' ", gamename);
+	db_query(query);
+	result = mysql_store_result(conn);
+	rows = mysql_num_rows(result);
+	mysql_free_result(result);
+	return rows;
+}
+
+
+void inc_time(char* gamename) {
+	int time = 0;
+
+	/* get current time */
+	sprintf(query, "select time from simulation where name = '%s' ", gamename);
+	db_query(query);
+	result = mysql_store_result(conn);
+	if (mysql_num_rows(result)) {
+		row = mysql_fetch_row(result);
+		time = atoi(row[0]);
+	}
+	mysql_free_result(result);
+
+
+	/* increment time */
+	sprintf(query, " update simulation set time = '%d' where name = '%s' ", ++time, gamename);
+	db_query(query);
+	if (mysql_affected_rows(conn) > 0) {
+		printf("time update successful\n");
+		printf("simulation time: %d\n", time);
+	} else {
+		printf("E: time update failed\n");
+	}
+
+}
+
+
+int main(int argc, char* argv[]) {
+	int i;
+	char* gamename;
+
+	/* init */
+	if (argc != 2) {
+		printf("usage: %s <game>\n", argv[0]);
+		exit(1);
+	}
+	gamename = argv[1];
+
+	printf("gamename: %s\n", gamename);
+
+	db_connect();
+
+	if (check_game(gamename) != 1) {
+		printf("game '%s' does not exist\n", gamename);
+		exit(1);
+	}
+
+	inc_time(gamename);
+
+/*
+	while ((row = mysql_fetch_row(result)) != NULL) {
+		for (i = 0; i < mysql_num_fields(result); i++) {
+			printf("%10s  ", row[i]);
+		}
+		printf("\n");
+	}
+	*/
+
+
+
+
+	db_close();
+
+	printf("the garten program\n");
+	return 0;
+}