Skip to content

Examples

Dr. Charles Bell edited this page Mar 29, 2020 · 11 revisions

This page presents documentation on how to start using the connector in your sketches. You will see helpful, brief introductions to the more commonly used example sketches as well as how to implement common solutions.

Example Sketches

The following are some of the more popular example sketches available with the connector. The following shows the complete list of example sketches. Be sure to start with one of these as you start using the connector. It is recommended you start with the first one named connect.ino.

Example MySQL Connector Sketches

You can find these examples sketches under the File | Examples | MySQL Connector Arduino menu.

Hello, MySQL! (connect.ino)

The simplest and thus the first example sketch you should attempt is the connect.ino sketch also referred to as the "Hello, MySQL!" sketch. This sketch shows you how to connect to a MySQL server. That's all it does - just a basic connect. If you can get this sketch to work, you've solved many of the common problems using the connector.

Code

#include <Ethernet.h>
#include <MySQL_Connection.h>

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress server_addr(10,0,1,35);  // IP of the MySQL *server* here
char user[] = "root";              // MySQL user login username
char password[] = "secret";        // MySQL user login password

EthernetClient client;
MySQL_Connection conn((Client *)&client);

void setup() {
  Serial.begin(115200);
  while (!Serial); // wait for serial port to connect
  Ethernet.begin(mac_addr);
  Serial.println("Connecting...");
  if (conn.connect(server_addr, 3306, user, password)) {
    delay(1000);
    // You would add your code here to run a query once on startup.
  }
  else
    Serial.println("Connection failed.");
  conn.close();
}

void loop() {
}

Discussion

Notice

Connect by Hostname (connect_by_hostname.ino)

Basic Insert (basic_insert.ino)

Complex Insert (complex_insert.ino)

Basic Select (basic_select.ino)

Complex Select (complex_select.ino)

Connect with WiFi (connect_wifi.ino)

Common Solutions

The following are code fragments and suggestions for solving some of the more common problems working with MySQL, data, and the connector.

Replacing Values in SQL Commands for Variables

Storing FLoating Point Values

Inserting Multiple Rows in a Single Query

Recording Datetime for a Row

Using a Lookup Table

Retriving a Field Value in a Variable

Preventing Dropped Connections

Using PROGMEM for Query Strings (SQL)

Connect/Reconnect

Reboot

Clone this wiki locally