MySQL Forums :: Connector/Arduino :: Reading values from mysql


Advanced Search

Re: Reading values from mysql
Posted by: Charles Bell ()
Date: April 25, 2014 06:52AM

Hello. There is an example of how to do this in the example code. Here is the method. Use this structure to run a query that returns results and processes them. Look at the line of code with the --> <-- marks. This is where we loop through the columns. Add your code here and remove the unneeded print statements.

Also, add a WHERE clause to target one specific row else you'll waste time looping through all of the rows.

/**
* do_query - execute a query and display results
*
* This method demonstrates how to execute a query, get the column
* names and print them, then read rows printing the values. It
* is a mirror of the show_results() example in the connector class.
*
* You can use this method as a template for writing methods that
* must iterate over rows from a SELECT and operate on the values read.
*
*/
void do_query(const char *q) {
column_names *c; // pointer to column values
row_values *r; // pointer to row values

// First, execute query. If it returns a value pointer,
// we have a result set to process. If not, we exit.
if (!my_conn.cmd_query(q)) {
return;
}

// Next, we read the column names and display them.
//
// NOTICE: You must *always* read the column names even if
// you do not use them. This is so the connector can
// read the data out of the buffer. Row data follows the
// column data and thus must be read first.
c = my_conn.get_columns();
for (int i = 0; i < c->num_fields; i++) {
Serial.print(c->fields->name);
if (i < c->num_fields - 1) {
Serial.print(",");
}
}
Serial.println();

// Next, we use the get_next_row() iterator and read rows printing
// the values returned until the get_next_row() returns NULL.
int num_cols = c->num_fields;
int rows = 0;
do {
r = my_conn.get_next_row();
if (r) {
rows++;
for (int i = 0; i < num_cols; i++) {
Serial.print(r->values);
if (i < num_cols - 1) {
--> Serial.print(", "); <---
}
}
Serial.println();
// Note: we free the row read to free the memory allocated for it.
// You should do this after you've processed the row.
my_conn.free_row_buffer();
}
} while (r);
Serial.print(rows);
Serial.println(" rows in result.");

// Finally, we are done so we free the column buffers
my_conn.free_columns_buffer();
}

Options: ReplyQuote


Subject Views Written By Posted
Reading values from mysql 2382 Steve Spence 04/17/2014 05:33PM
Re: Reading values from mysql 1420 Steve Spence 04/23/2014 09:47AM
Re: Reading values from mysql 1647 Charles Bell 04/25/2014 06:52AM
Re: Reading values from mysql 2082 Steve Spence 04/26/2014 12:12PM
Re: Reading values from mysql 1340 Charles Bell 04/30/2014 07:55AM
Re: Reading values from mysql 1365 Steve Spence 04/30/2014 08:08AM
Re: Reading values from mysql 1304 Steve Spence 04/30/2014 05:59PM
Re: Reading values from mysql 1300 Steve Spence 05/01/2014 05:00PM
Re: Reading values from mysql 1212 Charles Bell 05/07/2014 08:43AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.