I am trying to display my database data on an OLED LCD using MySQL_Connection.h and MySQL_Cursor.h by ChuckBell.
The link to this library is https://github.com/ChuckBell/MySQL_Connector_Arduino>
I was able to fetch data from mysql database successfully. However, I wish to store the data in a char array so that I can later display them on the OLED LCD. The problem is the value stored always returns garbage value. I know its something to do with char array pointer but after searching for so long, I still couldn't find the correct syntax. Below is the snippet of my code.
Start by setting up Wifi connection and mysql connection.
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
char query[] = "SELECT * FROM test.assetdemo WHERE RFID = \"048EB25A\""; //sql query
char* sqldata[11]; //array of char pointer to store the 11 data in the database
void setup(){
Serial.begin(115200);
internetConnect(ssid,pw); //connect to Wifi
conn.connect(server_addr, 3306, user, password); //connect to mysqldatabase
}
Then begin looping function to store and display the database data.
void loop(){
Serial.println("\nRunning SELECT and printing results\n");
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn); // Initiate the query class instance
cur_mem->execute(query); // Execute the query
row_values *row = NULL; // Read the rows and print them
do {
row = cur_mem->get_next_row();
if (row != NULL) {
for (int f = 0; f < cols->num_fields; f++) {
sqldata[f] = row->values[f];
Serial.print(f);
Serial.print(" ");
Serial.println(sqldata[f]); /*This works*/
}
Serial.println();
}
} while (row != NULL);
Serial.println(sqldata[0]); /*This return garbage value*/
delete cur_mem; // frees up memory used
delay(5000);
}
The output is shown as below Click here or view below. As you can see, the values are displayed correctly (except 8th which is of boolean type which i will change it later) in the do while loop. However, when I exit the loop and print the value again, it returns garbage value ⸮. sqldata[0] supposes to return 048EB25A.
Running SELECT and printing results
0 048EB25A
1 Blood Pressure Monitor
2 NA
3 WelchAllyn 503-0054-03
4 010720
5 NA
6 NA
7 Blood Pressure Cuff
8
9 Yes
10 1
⸮
The code snippet below shows the struct declaration in MySQL_Cursor.h
typedef struct {
char *db;
char *table;
char *name;
} field_struct;
// Structure for storing result set metadata.
typedef struct {
int num_fields; // actual number of fields
field_struct *fields[MAX_FIELDS];
} column_names;
// Structure for storing row data.
typedef struct {
char *values[MAX_FIELDS];
} row_values;
Part of me knew that sqldata[f] = row->values[f]; is causing the garbage value. The value will change as the pointer only points to the address. How can I store the variable statically so that after the do while, the value will persist?
Kind sirs and madams please explain this mystery.
PS: I am confused at array and pointer, even more when they involve struct.
row = cur_mem->get_next_row();invalidates the memory filled by the previousrow = cur_mem->get_next_row();. Don't copy the pointers but copy the strings.