Index: mysqlhistlog2300.c
===================================================================
--- mysqlhistlog2300.c	(revision 12)
+++ mysqlhistlog2300.c	(working copy)
@@ -13,7 +13,12 @@
  * 
  *  1,14 2006  July 19 (included in open2300 1.11)
  *  1.15 2007  July 19  EmilianoParasassi
- *             http://www.lavrsen.dk/twiki/bin/view/Open2300/MysqlPatch2 
+ *             http://www.lavrsen.dk/twiki/bin/view/Open2300/MysqlPatch2 
+ *	1.16 2008  May 15 Charles-Henri Hallard
+ *				-	Optimized version, check last mySql entries instead spending time for 
+  *					hard reading 175 records from WS2300 sometime just to update one
+ *				-	set program to me more verbose, because launching exe and wait before screen without
+ *					knowing what program is doing is very frustrating (from my point of view)
  */
 #include <mysql.h>
 #include "rw2300.h"
@@ -55,11 +60,17 @@
 int main(int argc, char *argv[])
 {
 	WEATHERSTATION ws2300;
-	MYSQL mysql, *mysql_connection;
+	MYSQL mysql, *mysql_connection;
+	MYSQL_RES *res;
+	MYSQL_ROW row;
 	int mysql_state;
 	char mysql_insert_stmt[512] = 
 		"INSERT INTO weather(datetime, temp_in, temp_out, dewpoint, rel_hum_in, "
-		"rel_hum_out, wind_speed, wind_angle, wind_direction,wind_chill, rain_total, rel_pressure)";
+		"rel_hum_out, wind_speed, wind_angle, wind_direction,wind_chill, rain_total, rel_pressure)";
+
+	// Query to get last record saved in database 
+	char mysql_select_stmt[128] = "select datetime from weather order by datetime desc limit 1";
+
 	char mysql_values_stmt[512], mysql_stmt[1024];
 
 	int interval, countdown, no_records;
@@ -127,6 +138,45 @@
 
 	time_lastlog = mktime(&time_lastlog_tm);
 
+	// Get Database last reccord
+	if (mysql_query(&mysql,mysql_select_stmt))
+	{
+		fprintf(stderr, "%d: %s \n",
+		mysql_errno(&mysql), mysql_error(&mysql));
+		exit(EXIT_FAILURE);
+	}
+
+	// Prepare to retrieve MySQL data
+	res = mysql_store_result(&mysql);
+
+	// To be clean but should only have one record here since "limit 1" in sql query
+	while ((row = mysql_fetch_row(res)))
+	{
+		// this function does not exists under windows, so old scanf
+		// strptime( row[0], "\"%Y-%m-%d %H:%M:%S\"", &time_lastlog_tm);
+		sscanf( row[0] ,"%04d-%02d-%02d %02d:%02d:%02d", 
+						&time_lastlog_tm.tm_year,
+						&time_lastlog_tm.tm_mon,
+						&time_lastlog_tm.tm_mday,
+						&time_lastlog_tm.tm_hour,
+						&time_lastlog_tm.tm_min,
+						&time_lastlog_tm.tm_sec );
+
+		// Correct data
+		time_lastlog_tm.tm_year -= 1900;
+		time_lastlog_tm.tm_mon--;
+		time_lastlog_tm.tm_isdst = -1;
+
+		// Now we're on
+		time_lastlog = mktime(&time_lastlog_tm);
+
+		printf( "\nLast reccord in database was on %s", row[0]);
+
+	}
+
+	// Clean up
+	mysql_free_result(res);
+
 
 	// Start reading the history from the WS2300
 		
@@ -146,7 +196,13 @@
 	pressure_term = pressure_correction(ws2300, config.pressure_conv_factor);
 
 	new_records = (int)difftime(time_lastrecord,time_lastlog) / (60 * interval);
-	
+
+	if (new_records >0 )
+		printf( "\nNeed to update %d record%c", new_records, new_records > 1 ? 's' : ' ' );
+	else
+		printf( "\nDatabase content is already up to date");
+
+
 	if (new_records > 0xAF)
 		new_records = 0xAF;
 		
@@ -199,6 +255,8 @@
 			sprintf(mysql_values_stmt,"%s,%.1f",mysql_values_stmt, windchill);
 			sprintf(mysql_values_stmt,"%s,%.2f",mysql_values_stmt, rain);
 			sprintf(mysql_values_stmt,"%s,%.3f)",mysql_values_stmt, pressure + pressure_term);
+
+			printf( "\nUpdating record #%03d/%03d for %s", i, new_records, datestring);
 
 			// Build SQL string
 			sprintf(mysql_stmt,"%s %s",mysql_insert_stmt, mysql_values_stmt);
