MySQL Forums
Forum List  »  Connector/Arduino

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)) {

// 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++) {
if (i < c->num_fields - 1) {

// 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) {
for (int i = 0; i < num_cols; i++) {
if (i < num_cols - 1) {
--> Serial.print(", "); <---
// Note: we free the row read to free the memory allocated for it.
// You should do this after you've processed the row.
} while (r);
Serial.println(" rows in result.");

// Finally, we are done so we free the column buffers

Options: ReplyQuote

Written By
April 17, 2014 05:33PM
April 23, 2014 09:47AM
Re: Reading values from mysql
April 25, 2014 06:52AM
April 26, 2014 12:12PM
April 30, 2014 07:55AM
April 30, 2014 08:08AM
April 30, 2014 05:59PM
May 01, 2014 05:00PM
May 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.