Envoyer donnée dans une base de données

Bonjour a tous,

J'essaie d'envoyer une donnée dans une base de données sur mon hébergement.
Apparemment j'arrive a me connecter a la base de données, mais pas a y enregistrer ma donnée, et je ne vois pas ou ça bloque.

Voici mon code:


#include <ESP8266WiFi.h>           // Use this for WiFi instead of Ethernet.h
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>

IPAddress server_addr(1,4,31,1);  // IP of the MySQL *server* here
char user[] = "user";              // MySQL user login username
char password[] = "MdP";        // MySQL user login password

// Sample query
char INSERT_SQL[] = "INSERT INTO test_arduino.hello_arduino (message) VALUES ('Hello, Arduino!')";

// WiFi card example
char ssid[] = "SSID";         // your SSID
char pass[] = "MdP";     // your SSID Password

WiFiClient client;                 // Use this for WiFi instead of EthernetClient
MySQL_Connection conn(&client);
MySQL_Cursor* cursor;

void setup()
{
  Serial.begin(115200);
  while (!Serial); // wait for serial port to connect. Needed for Leonardo only

  // Begin WiFi section
  Serial.printf("\nConnecting to %s", ssid);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  // print out info about the connection:
  Serial.println("\nConnected to network");
  Serial.print("My IP address is: ");
  Serial.println(WiFi.localIP());

  Serial.print("Connecting to SQL...  ");
  if (conn.connect(server_addr, 3306, user, password))
    Serial.println("OK.");
  else
    Serial.println("FAILED.");
  
  // create MySQL cursor object
  cursor = new MySQL_Cursor(&conn);
}

void loop()
{
  if (conn.connected())
    cursor->execute(INSERT_SQL);

  delay(5000);
}

et voici ce que j'ai dans le moniteur:

Connecting to SSID.............
Connected to network
My IP address is: 192.168.0.24
Connecting to SQL...  ...trying...
Connected to server version 5.5.5-10.5.13-MariaDB-cll-lve
OK.
Error: 101 = INSERT command denied to user 'u984373661_treza88'@'85.168.31.223' for table 'hello_arduino'.
Error: 101 = INSERT command denied to user 'u984373661_treza88'@'85.168.31.223' for table 'hello_arduino'.
Error: 101 = INSERT command denied to user 'u984373661_treza88'@'85.168.31.223' for table 'hello_arduino'.
Error: 101 = INSERT command denied to user 'u984373661_treza88'@'85.168.31.223' for table 'hello_arduino'.
Error: 101 = INSERT command denied to user 'u984373661_treza88'@'85.168.31.223' for table 'hello_arduino'

Merci d'avance

A priori l'utilisateur que tu utilise n'a pas les droits sur la table hello_arduino

As tu vérifié les droits associés à to utilisateur?

comment avez vous créé l'utilisateur ? avec localhost?
CREATE USER 'UTILISATEUR'@'localhost' IDENTIFIED BY 'PWD';

si c'est le cas il faudra sans doute lui donner les droits
GRANT ALL PRIVILEGES ON *.* TO 'UTILISATEUR'@'%' IDENTIFIED BY 'PWD' WITH GRANT OPTION;

un FLUSH PRIVILEGES; peut aussi être utile pour que ce soit pris en compte

Sinon comme vous êtes à distance vous pouvez modifier les droits d'accès

GRANT ALL PRIVILEGES
ON database.*
TO 'UTILISATEUR'@'%'
IDENTIFIED BY 'pwd';

Merci à vous deux, j'ai trouvé mon erreur, je ne ciblais pas la bonne base de données dans mon "INSERT INTO"

Tout est rentrer dans l'ordre

Une autre question a part, est il possible de créer un programme avec les éléments de connexion a ma base de données dans un autre programme est de l'appeler comme une librairie par exemple?
Ceci pour alléger mon code principale.

pas dans un autre programme, car il n'y en a qu'un seul qui tourne sur votre arduino, mais rien ne vous empêche de structurer votre code en plusieurs fichiers.

si vous les appelez tous .ino alors l'IDE va les concaténer avant de compiler, si vous faites "propre" avec un .h et un .cpp alors les règles habituelles de compilation s'appliqueront, il faudra importer le .h dans le .ino pour connaître ce que vous voulez rendre accessible dans le fichier .cpp

Merci pour le complément d'information, je vais voir si j'arrive a bien manipuler ces fichiers.

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