日本語 English

How to INSERT Data into MySQL

Explains how to INSERT PLC data into a MySQL database using the DB connection function of the OMRON NX/NJ series.
Using DB_Connect, DB_CreateMapping and DB_Insert, equipment data and production results can be saved to a database.

mysql

How to create the MySQL table

Create a table in MySQL

Create a table named wx_data
Create the table by running the following SQL on the mysql command line or as a query in MySQL Workbench

The created table looks like this
sample_time device_id device_data
... ... ...

Creating the PLC program

DB connection settings

Settings for connecting to the DB
Add a connection setting named "DBConnection01" under "DB Connection Settings" below DB Connection in the host connection settings
Select MySQL as the database type, set the IP address of the DB server and the database name, and set the user name and password for connecting to that DB

DBConnection
Sysmac Studio DBConnection settings screen

Ladder logic

MySQL Insert ladder
MySQL Insert sample ladder
  1. Turn Trigger1 ON to turn ON the DBInsert executing flag
  2. Connect to the DB with DB_Connect
    For DBConnectionName, enter "DBConnection01" created in the DBConnection settings
  3. Associate the structure (DBInsertData) with the table (wx_data) as the Insert type with DB_CreateMapping
    Make the structure match the table as follows
  4. Set the data to add to the table
  5. Execute DB_Insert to add the data
  6. When processing completes, close the connection to the DB with DB_Close
    Also close it when an error occurs in the Mapping or Insert instruction

Checking the data in the DB

Check on the MySQL command line

Frequently Asked Questions (FAQ)

Can it connect to databases other than MySQL?

Yes, any supported DB such as PostgreSQL or SQL Server.

Can it be used with models other than the NX102?

Yes, with NX/NJ series models that support the DB connection function.

What if DB_Insert returns an error?

Check the DB connection settings, user permissions and table definition.

Download

Comments