Hi,
How to write two SELECT queries to different tables in the same database.
In the case of INSERT and UPDATE there are no problems. In the case of SELECT, only the one which is first in order is executed. On the second one the program expires, but after commenting out the first query the second query executes correctly and the system works.
I am using NodeMCU v.3, MySQL Connector/Arduino
I added checkpoints in the code so that you can catch where the problem occurs.
The second query should be executed on a table whose part of the name is the result of the first query, so for the test in the second case, I wrote the name of the table rigidly.
Here is my code.
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
char query[100];
sprintf(query, "SELECT rok FROM rok_pomiarow ORDER BY id_rok_pomiarow DESC LIMIT 1");
cur_mem->execute(query);
Serial.println("Control point: 3");
column_names *cols = cur_mem->get_columns();
Serial.println("Control point: 3A");
for (int f = 0; f < cols->num_fields; f++) {
Serial.print(cols->fields[f]->name);
Serial.println("----------------");
if (f < cols->num_fields - 1) {
Serial.print(",");
}
}
Serial.println("Control point: 3B");
row_values *row = NULL;
Serial.println("Control point: 3C");
row = cur_mem->get_next_row();
Serial.println("Control point: 3D");
int year = atoi(row->values[0]);
Serial.println("Control point: 3E");
Serial.println("--------------------------------------------");
char table_name[20];
sprintf(table_name, "pomiary_%d", year);
Serial.println("Control point: 3F");
Serial.println("Nazwa tabeli z pomiarami: " + String(table_name));
Serial.println("Control point: 3G");
delete cur_mem;
Serial.println("Control point: 3H");
Serial.println("--------------------------------------------");
MySQL_Cursor *cur_mem2 = new MySQL_Cursor(&conn);
Serial.println("Control point: 4A");
char query2[100];
//sprintf(query2, "SELECT temperatura, wilgotnosc, cisnienie, oswietlenie FROM %s ORDER BY id DESC LIMIT 1", table_name);
sprintf(query2, "SELECT temperatura, wilgotnosc, cisnienie, oswietlenie FROM pomiary_2024 ORDER BY id DESC LIMIT 1");
cur_mem2->execute(query2);
Serial.println("Control point: 4B");
column_names *cols2 = cur_mem2->get_columns();
Serial.println("Control point: 4C");
for (int f = 0; f < cols2->num_fields; f++) {
Serial.print(cols2->fields[f]->name);
Serial.println("----------------");
if (f < cols2->num_fields - 1) {
Serial.print(",");
}
}
Serial.println("Control point: 4D");
row_values *row2 = NULL;
Serial.println("Control point: 4E");
row2 = cur_mem2->get_next_row();
Serial.println("Control point: 4F");
Serial.println("ot = " + String(row2->values[0]));
Serial.println("ow = " + String(row2->values[1]));
Serial.println("oc = " + String(row2->values[2]));
Serial.println("oo = " + String(row2->values[3]));
Serial.println("Control point: 4G");
float old_temperatura = atof(row2->values[0]);
long old_wilgotnosc = atol(row2->values[1]);
long old_cisnienie = atol(row2->values[2]);
int old_oswietlenie = atoi(row2->values[3]);
Serial.println("Control point: 4H");
delete cur_mem2;
Serial.println("Control point: 4I");
The result of the operation
Connected to server version 5.5.5-10.4.24-MariaDB-cll-lve
Połaczono z bazą MySQL
Control point: 1
Control point: 2
Control point: 3
Control point: 3A
rok----------------
Control point: 3B
Control point: 3C
Control point: 3D
Control point: 3E
Control point: 3F
Nazwa tabeli z pomiarami: pomiary_2024
Control point: 3G
Control point: 3H
Control point: 4A
Control point: 4B
--------------- CUT HERE FOR EXCEPTION DECODER ---------------
Exception (29):
epc1=0x4021ab7d epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
After commenting out the first query, the result of the operation looks like this
Connected to server version 5.5.5-10.4.24-MariaDB-cll-lve
Połaczono z bazą MySQL
Control point: 1
Control point: 2
Control point: 4A
Control point: 4B
Control point: 4C
temperatura----------------
,wilgotnosc----------------
,cisnienie----------------
,oswietlenie----------------
Control point: 4D
Control point: 4E
Control point: 4F
ot = 10.3
ow = 88
oc = 1000
oo = 0
Control point: 4G
Control point: 4H
Control point: 4I
Disconnected.
I've tried various ways, but it didn't work so please help me with this issue.