garten

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 diff
     1.1 --- a/weather.c	Wed Jul 23 11:40:45 2008 +0200
     1.2 +++ b/weather.c	Wed Jul 23 11:41:38 2008 +0200
     1.3 @@ -1,6 +1,5 @@
     1.4  #include <stdio.h>
     1.5  #include <stdlib.h>
     1.6 -#include <mysql.h>
     1.7  #include <time.h>
     1.8  
     1.9  #include "db.h"
    1.10 @@ -45,11 +44,20 @@
    1.11  
    1.12  	sprintf(query,
    1.13  			" select temp, sun, rain, wind, hum from weather "
    1.14 -			" where game_id = '%d' "
    1.15  			" order by tick desc "
    1.16  			" limit %d "
    1.17 -			, gameid, nlast);
    1.18 +			, nlast);
    1.19  	db_query(query);
    1.20 +	for (i = 0; i < nlast && sqlite3_step(stmt) == SQLITE_ROW; i++) {
    1.21 +		lastw[i].temp = sqlite3_column_double(stmt, 0);
    1.22 +		lastw[i].sun = sqlite3_column_double(stmt, 1);
    1.23 +		lastw[i].rain = sqlite3_column_double(stmt, 2);
    1.24 +		lastw[i].wind = sqlite3_column_double(stmt, 3);
    1.25 +		lastw[i].hum = sqlite3_column_double(stmt, 4);
    1.26 +	}
    1.27 +	sqlite3_finalize(stmt);
    1.28 +
    1.29 +	/*
    1.30  	result = mysql_store_result(conn);
    1.31  	for (i = 0; i < nlast && (row = mysql_fetch_row(result)); i++) {
    1.32  		lastw[i].temp = atof(row[0]);
    1.33 @@ -59,6 +67,7 @@
    1.34  		lastw[i].hum = atof(row[4]);
    1.35  	}
    1.36  	mysql_free_result(result);
    1.37 +	*/
    1.38  
    1.39  }
    1.40  
    1.41 @@ -72,16 +81,20 @@
    1.42  }
    1.43  
    1.44  
    1.45 -int setweather(struct weather* w) {
    1.46 +void setweather(struct weather* w) {
    1.47  	char query[512];
    1.48  	sprintf(query,
    1.49  			" insert into weather "
    1.50 -			" (tick, game_id, temp, sun, rain, wind, hum) "
    1.51 -			" values ('%d', '%d', '%f', '%f', '%f', '%f', '%f') "
    1.52 -			, gametime, gameid, w->temp, w->sun, w->rain, w->wind, w->hum);
    1.53 +			" (tick, temp, sun, rain, wind, hum) "
    1.54 +			" values ('%d', '%f', '%f', '%f', '%f', '%f') "
    1.55 +			, gametime, w->temp, w->sun, w->rain, w->wind, w->hum);
    1.56  	db_query(query);
    1.57  	puts(query);
    1.58 -	return mysql_affected_rows(conn);
    1.59 +	if (sqlite3_step(stmt) == SQLITE_DONE) {
    1.60 +		printf("weather successful inserted\n");
    1.61 +	} else {
    1.62 +		printf("error: weather insertion failed: %s\n", sqlite3_errmsg(db));
    1.63 +	}
    1.64  }
    1.65  
    1.66  
    1.67 @@ -90,13 +103,13 @@
    1.68  
    1.69  	/* init */
    1.70  	if (argc != 2) {
    1.71 -		printf("usage: %s <game>\n", argv[0]);
    1.72 +		printf("usage: %s <database>\n", argv[0]);
    1.73  		exit(1);
    1.74  	}
    1.75 -	gamename = argv[1];
    1.76 +	database = argv[1];
    1.77  
    1.78  	db_connect();
    1.79 -	check_game();
    1.80 +	read_time();
    1.81  
    1.82  	srand((unsigned int) time(NULL));
    1.83  
    1.84 @@ -105,17 +118,8 @@
    1.85  	struct weather lastn[Nlast];
    1.86  
    1.87  	getlastweather(lastn, Nlast);
    1.88 -
    1.89  	genweather(&w, lastn, Nlast, May);
    1.90 -
    1.91 -	if (setweather(&w) > 0) {
    1.92 -		printf("weather successful inserted\n");
    1.93 -	} else {
    1.94 -		printf("E: weather insertion failed\n");
    1.95 -	}
    1.96 -
    1.97 -/* 	set_weather(); */
    1.98 -
    1.99 +	setweather(&w);
   1.100  
   1.101  	db_close();
   1.102