1111
1212 Built by Khoi Hoang https://github.com/khoih-prog/MySQL_MariaDB_Generic
1313 Licensed under MIT license
14- Version: 1.0.1
14+ Version: 1.0.2
1515
1616 Version Modified By Date Comments
1717 ------- ----------- ---------- -----------
1818 1.0.0 K Hoang 13/08/2020 Initial coding/porting to support nRF52, SAM DUE and SAMD21/SAMD51 boards using W5x00 Ethernet
19- (using Ethernet, EthernetLarge, Ethernet2, Ethernet3 library) and WiFiNINA
19+ (Ethernet, EthernetLarge, Ethernet2, Ethernet3 library), WiFiNINA and ESP8266/ESP32-AT shields
2020 1.0.1 K Hoang 18/08/2020 Add support to Ethernet ENC28J60. Fix bug, optimize code.
21+ 1.0.2 K Hoang 20/08/2020 Fix crashing bug when timeout. Make code more error-proof. Drop support to ESP8266_AT_Webserver.
2122 **********************************************************************************************************************************/
2223
2324/*
@@ -72,7 +73,7 @@ char default_table[] = "city"; //"test_arduino";
7273// Notice the "%lu" - that's a placeholder for the parameter we will
7374// supply. See sprintf() documentation for more formatting specifier
7475// options
75- unsigned long QUERY_POPULATION = 8000000 ;
76+ unsigned long QUERY_POPULATION = 800000 ;
7677
7778const char QUERY_POP[] = " SELECT name, population FROM world.city WHERE population < %lu ORDER BY population DESC LIMIT 12;" ;
7879
@@ -249,17 +250,22 @@ void runQuery(void)
249250 // to allocate one buffer for all formatted queries or allocate the
250251 // memory as needed (just make sure you allocate enough memory and
251252 // free it when you're done!).
252- sprintf (query, QUERY_POP, QUERY_POPULATION);
253+ sprintf (query, QUERY_POP, QUERY_POPULATION + (( millis () % 100000 ) * 10 ) );
253254 Serial.println (query);
254255
255256 // Initiate the query class instance
256- MySQL_Query * query_mem = new MySQL_Query (&conn);
257+ MySQL_Query query_mem = MySQL_Query (&conn);
257258
258259 // Execute the query
259- query_mem->execute (query);
260+ // KH, check if valid before fetching
261+ if ( !query_mem.execute (query) )
262+ {
263+ Serial.println (" Querying error" );
264+ return ;
265+ }
260266
261267 // Fetch the columns and print them
262- column_names *cols = query_mem-> get_columns ();
268+ column_names *cols = query_mem. get_columns ();
263269
264270 for (int f = 0 ; f < cols->num_fields ; f++)
265271 {
@@ -278,7 +284,7 @@ void runQuery(void)
278284
279285 do
280286 {
281- row = query_mem-> get_next_row ();
287+ row = query_mem. get_next_row ();
282288
283289 if (row != NULL )
284290 {
@@ -295,9 +301,6 @@ void runQuery(void)
295301 Serial.println ();
296302 }
297303 } while (row != NULL );
298-
299- // Deleting the cursor also frees up memory used
300- delete query_mem;
301304}
302305
303306void loop ()
@@ -319,5 +322,5 @@ void loop()
319322 Serial.println (" \n Sleeping..." );
320323 Serial.println (" ================================================" );
321324
322- delay (60000 );
325+ delay (10000 );
323326}
0 commit comments