diff weather.c @ 11:176ee28e7464

switched from mysql to sqlite; (+ some cleanups)
author meillo@marmaro.de
date Wed, 23 Jul 2008 11:41:38 +0200
parents 9bd0a2100694
children 8db6497d6065
line wrap: on
line diff
--- a/weather.c	Wed Jul 23 11:40:45 2008 +0200
+++ b/weather.c	Wed Jul 23 11:41:38 2008 +0200
@@ -1,6 +1,5 @@
 #include <stdio.h>
 #include <stdlib.h>
-#include <mysql.h>
 #include <time.h>
 
 #include "db.h"
@@ -45,11 +44,20 @@
 
 	sprintf(query,
 			" select temp, sun, rain, wind, hum from weather "
-			" where game_id = '%d' "
 			" order by tick desc "
 			" limit %d "
-			, gameid, nlast);
+			, nlast);
 	db_query(query);
+	for (i = 0; i < nlast && sqlite3_step(stmt) == SQLITE_ROW; i++) {
+		lastw[i].temp = sqlite3_column_double(stmt, 0);
+		lastw[i].sun = sqlite3_column_double(stmt, 1);
+		lastw[i].rain = sqlite3_column_double(stmt, 2);
+		lastw[i].wind = sqlite3_column_double(stmt, 3);
+		lastw[i].hum = sqlite3_column_double(stmt, 4);
+	}
+	sqlite3_finalize(stmt);
+
+	/*
 	result = mysql_store_result(conn);
 	for (i = 0; i < nlast && (row = mysql_fetch_row(result)); i++) {
 		lastw[i].temp = atof(row[0]);
@@ -59,6 +67,7 @@
 		lastw[i].hum = atof(row[4]);
 	}
 	mysql_free_result(result);
+	*/
 
 }
 
@@ -72,16 +81,20 @@
 }
 
 
-int setweather(struct weather* w) {
+void setweather(struct weather* w) {
 	char query[512];
 	sprintf(query,
 			" insert into weather "
-			" (tick, game_id, temp, sun, rain, wind, hum) "
-			" values ('%d', '%d', '%f', '%f', '%f', '%f', '%f') "
-			, gametime, gameid, w->temp, w->sun, w->rain, w->wind, w->hum);
+			" (tick, temp, sun, rain, wind, hum) "
+			" values ('%d', '%f', '%f', '%f', '%f', '%f') "
+			, gametime, w->temp, w->sun, w->rain, w->wind, w->hum);
 	db_query(query);
 	puts(query);
-	return mysql_affected_rows(conn);
+	if (sqlite3_step(stmt) == SQLITE_DONE) {
+		printf("weather successful inserted\n");
+	} else {
+		printf("error: weather insertion failed: %s\n", sqlite3_errmsg(db));
+	}
 }
 
 
@@ -90,13 +103,13 @@
 
 	/* init */
 	if (argc != 2) {
-		printf("usage: %s <game>\n", argv[0]);
+		printf("usage: %s <database>\n", argv[0]);
 		exit(1);
 	}
-	gamename = argv[1];
+	database = argv[1];
 
 	db_connect();
-	check_game();
+	read_time();
 
 	srand((unsigned int) time(NULL));
 
@@ -105,17 +118,8 @@
 	struct weather lastn[Nlast];
 
 	getlastweather(lastn, Nlast);
-
 	genweather(&w, lastn, Nlast, May);
-
-	if (setweather(&w) > 0) {
-		printf("weather successful inserted\n");
-	} else {
-		printf("E: weather insertion failed\n");
-	}
-
-/* 	set_weather(); */
-
+	setweather(&w);
 
 	db_close();