Help sending into MYSQL :(

I have tried many ways and followed the example but cannot insert into mysql :(( hope everyone can help me

#include <Arduino.h>
#include <Ethernet.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <rdm6300.h>

byte mac_addr[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip_addr(10, 84, 3, 30);    // Arduino static IP address
IPAddress server_addr(10, 84, 3, 91); // MySQL server IP address
char user[] = "test";                // MySQL username
char password[] = "123456789";       // MySQL password
char db[] = "datatool";               // Database name
EthernetClient client;
MySQL_Connection conn((Client *)&client);

#define RDM6300_RX_PIN 5
Rdm6300 rdm6300;

MySQL_Cursor *cur_mem; // Declare MySQL cursor at a higher scope

void setup() {
  Serial.begin(115200);
  while (!Serial); // Wait for Serial connection
  Ethernet.begin(mac_addr, ip_addr);
  Serial.println("Connecting...");
  delay(1000);

  if (conn.connect(server_addr, 3306, user, password, db)) {
    Serial.println("Connection success!.");
    rdm6300.begin(RDM6300_RX_PIN);
    cur_mem = new MySQL_Cursor(&conn); // Create MySQL cursor
  } else {
    Serial.println("Connection failed.");
  }
}

void loop() {
  // Check if a new RFID tag is detected
  if (rdm6300.get_new_tag_id()) {
    // Get the tag ID as an unsigned long
    unsigned long tagId = rdm6300.get_tag_id();

    // Construct the SQL query
    String query = "SELECT employeeName, employeeID, section FROM employee WHERE IDcard = '" + String(tagId) + "'";

    // Execute the query
    cur_mem->execute(query.c_str());

    // Fetch the columns
    column_names *cols = cur_mem->get_columns();

    // Fetch the rows
    row_values *row = cur_mem->get_next_row();

    if (row != NULL) {
      // Access and print the values of employeeName, employeeID, and section directly
      Serial.print("employeeName: ");
      Serial.println(row->values[0]);
      Serial.print("employeeID: ");
      Serial.println(row->values[1]);
      Serial.print("section: ");
      Serial.println(row->values[2]);

      String insertQuery = "INSERT INTO ricedata (Timestamp, employeeID, employeeName, Section) VALUES (NOW(),'" + String(row->values[1]) + "', '" + String(row->values[0]) + "', '" + String(row->values[2]) + "')";
      cur_mem->execute(insertQuery.c_str())
      row = cur_mem->get_next_row();

    } else {
      // Print the RFID tag ID when the employee is not found
      Serial.print("Employee not found for RFID tag ID: ");
      Serial.println(tagId);
    }  
  }
}

I can't help you but it will help if you describe the behaviour of your sketch. E.g.

  1. Does the sketch successfully connect to the DB?
  2. Can you retrieve the record?
  3. Using examples of the libraries, can you retrieve/insert records?
  4. Etc.

I'm sure the DB connection was successful because I can get information when swiping the RIFD card, I also tried insert_example and the insert was successful! The sketch I'm doing is that when an RFID card is swiped, the employee's information will appear on the screen, then about 2 seconds later, the information will be recorded in the database, including the date and time of the card swipe.

Hi @hai_minh96 ,
Welcome to the forum..

I would start by Serial printing this string out just after you build it..

and then refresh my memory on MYSQL, when shooting in values, do only strings need quotes, doubles quotes or single??
i'm use to firebird sql server, single quotes on strings and no quotes on numerical data..
when you print out the string, paste it into your SQL admin software and try to execute it..

good luck.. ~q

1 Like

Sorry my English is not very good, I don't quite understand what you mean! I've only been coding arduino for 2 weeks, so can you give me a more specific example? Thanks for your help

So ... What is not working? Please clarify!

  String insertQuery = "INSERT INTO ricedata (Timestamp, employeeID, employeeName, Section) VALUES (NOW(),'" + String(row->values[1]) + "', '" + String(row->values[0]) + "', '" + String(row->values[2]) + "')";

I don't know if I code this right, but it doesn't seem to work because when I go to the database, I don't see the data being recorded.

does it even compile without the trailing semi colon on this line?

sr because I copied too fast so I lost the ";" ,I don't understand why I can't write data to the database even though I've tried many ways :frowning:

test your system with a demo code to see if the network and your set-up is working

add print statements and look at the log on the server side to see if the connection is successful

share more info with us

1 Like


This is my code, after swiping the RFID card, the information is output but not written to the DB

Here is the example code I test the function of writing data to the DB

It is not right, because the ricedata table should not contain the employee name or section, only the employee id. The other data can be retrieved from the employee table when required. That would be good database design. What if the employee's name changes, or they move to another section? You will have more than one table to update in order to keep data consistent between tables.

But this is not the reason why your code is not working at the moment.

build a canned query not using any tagId

    cur_mem->execute("SELECT employeeName FROM employee");

an print out whatever is returned

Thank you everyone for helping me find the cause! Because I use Uno, the memroy is not enough, so I can't write data to the DB. After I changed to MEGA2560, it was able to write.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.