0% found this document useful (0 votes)
181 views37 pages

Functionality: Siemens S7-1200/1500

This document provides information about using a PDSql Library My to connect a Siemens S7-1200/1500 PLC directly to a MySQL database. It discusses the hardware and software requirements, supported MySQL functions and data types, program blocks, PLC data types, errors, licensing, and provides installation and usage examples.

Uploaded by

Victor Mendoza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
181 views37 pages

Functionality: Siemens S7-1200/1500

This document provides information about using a PDSql Library My to connect a Siemens S7-1200/1500 PLC directly to a MySQL database. It discusses the hardware and software requirements, supported MySQL functions and data types, program blocks, PLC data types, errors, licensing, and provides installation and usage examples.

Uploaded by

Victor Mendoza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Revision 05/2020

Siemens S7-1200/1500
TIA Portal (V13, V14, V15, V16)

Functionality
Implementation into user project

PDSql Library My v1.3.4

User guide

www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

Contents
1 Introduction .....................................................................................................................................................3
1.1 Hardware requirements .............................................................................................................................................. 3
1.2 Software requirements................................................................................................................................................. 3
1.3 Supported MySQL functions ....................................................................................................................................... 3
1.4 Supported MySQL data types ..................................................................................................................................... 4
1.5 Program blocks ................................................................................................................................................................ 5
1.5.1 Function block FB_PDSql_My ............................................................................................................................ 5
1.5.2 Data block DB_PDSql_My..................................................................................................................................... 6
1.6 PLC data types .................................................................................................................................................................. 7
1.6.1 tConnection ............................................................................................................................................................... 7
1.6.2 tCmd ............................................................................................................................................................................. 8
1.6.3 tLogin........................................................................................................................................................................... 8
1.6.4 tStat .............................................................................................................................................................................. 9
1.6.5 tLicense....................................................................................................................................................................... 9
1.6.6 tQuery.......................................................................................................................................................................... 9
1.6.7 tRow_1200 ............................................................................................................................................................. 10
1.6.8 tRow_1500 ............................................................................................................................................................. 10
1.6.9 tColumn_1200 ....................................................................................................................................................... 11
1.6.10 tColumn_1500 .................................................................................................................................................... 11
1.6.11 tSqlTable_1200 .................................................................................................................................................. 11
1.6.12 tSqlTable_1500 .................................................................................................................................................. 12
1.7 Errors ................................................................................................................................................................................ 12
1.8 License / Demo ............................................................................................................................................................. 13

2 Instalation ........................................................................................................................................................ 14
2.1 MySQL Community Server 8.0.19 ......................................................................................................................... 14
2.1.1 Host access settings for your MySQL account ......................................................................................... 21
2.2 Instalation PDSql Library My to TIA Portal .................................................................................................. 23

3 Examples ............................................................................................................................................................ 28
3.1 Create a table ................................................................................................................................................................. 28
3.2 SELECT row from table ............................................................................................................................................. 29
3.3 INSERT row to the table ........................................................................................................................................... 32
3.4 UPDATE row in table ................................................................................................................................................. 34
3.5 DELETE row from table ............................................................................................................................................ 36

2
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

1 Introduction
PDSql Library My allows you to connect your PLC Siemens S7-1200 or S7-1500 system directly
to MySQL Database. With this library you are able to read and write data from/to MySQL Database.
As the communication between the PLC and the MySQL Server takes place directly you don't need
any PC as a broker in the communication between the PLC and MySQL server.

1.1 Hardware requirements


PDSql library My was built for PLC Siemens S7-1200 (fw 4.2+) and S7-1500 (fw 1.8+).

1.2 Software requirements


Main requirements for this library is TIA Portal V13+ and MySQL Server (5.x or higher).
TIA Portal is available only with paid licence. MySQL Community Server 8.0.19 is available as free
version with some limitations -> https://dev.mysql.com/downloads/mysql/
There is also MariaDB (5.x, 10.x) compatible with MySQL -> https://downloads.mariadb.org/

1.3 Supported MySQL functions


This library allows you to execute all the basic MySQL commands like

• SELECT
• INSERT
• DELETE
• UPDATE
• Execute Stored PROCEDURE

However, in addition to this basic MySQL commands, other MySQL commands can be also
executed depending on string content in query input. However, with some commands of this
type, the query may end up with an error.

3
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

1.4 Supported MySQL data types


Supported are only datatypes listed in the Table 1-1. Any other datatype that is not
listed in Table 1.1 will either convert its value to a string or cause the query to quit with an error.

Table 1-1: MySQL data types


Datatype
S7-1200 S7-1500
MySQL PLC
bit Bool • •
text String( )
1
• •
tinytext String( )
1
• •
varchar String(1) • •
char String(1) • •
tinyint DInt/LInt •2 •3
smallint DInt/LInt •2 •3
Int DInt/LInt •2 •3
mediumint DInt/LInt •2 •3
bigint DInt/LInt •2 •3
float LReal • •
double LReal • •
real LReal • •
decimal LReal • •
datetime DTL • •
date DTL • •
time DTL • •
timestamp DTL • •
year DTL • •

1 The default is a maximum string length of 50 characters


* If the string length of the SQL data type is greater than the string length in the PLC, so the rest of
the text will be cut off.
2 PLC data type DInt
3 PLC data type LInt

4
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

1.5 Program blocks

1.5.1 Function block FB_PDSql_My

The whole concept of this library was built on the ease of implementation into user projects.
Therefore, all functions for communication with MySQL Server are contained in only one
function block FB_PDSql_My. This block is also associated with one instance of the data block
DB_PDSql_My_IDB. The last one is the data block DB_PDSql_My which provides all parameters
and the interface between the user application and the PDSql library. It contains the resulting
SQL table obtained after SELECT command. These blocks are supplied in 2 versions for S7-1200
and S7-1500.

S7-1200 S7-1500

Function block
Instance data block
Interface data block

The only 3 program blocks you need to add


to your project to implementation of MySQL functionality

5
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

Table 1-2 : FB_PDSql_My interface


Type Name Data type Comment

Connection tConnection MySQL server connection parameters


Login tLogin Login parameters
Input
Licence tLicence Runtime license key
Query tQuery MySQL query string
Output Stat tStat Status bits, messages and errors
Cmd tCmd Commands Connect, Disconnect, ExecuteQuery
InOut MySQL parameters and table contents as a result
SqlTable tSqlTable (1200/1500)
of the SELECT statement

Recommended steps for executing MySQL commands from a user application

1. Create a string of MySQL statements and write it to Query


2. Set the Cmd.ExecuteQuery signal to TRUE
3. Wait for signal Stat.ExecutedOK or Stat.Error
4. If the Stat.Error signal is TRUE, repeat the action from step 1.
If the Stat.ExecutedOK signal is TRUE application can continue to prepare another MySQL
statement if required and repeat from step 1.

The user application can fully control the Connect or Disconnect signals, but it is sufficient to
control only ExecuteQuery signal which ensure the connection automatically if needed.

1.5.2 Data block DB_PDSql_My

Data block DB_PDSql_My is composed of all the data types described in chapter 1.6. All
communication between the user application and the MySQL server is done through this data block.

S7-1200 S7-1500

6
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

1.6 PLC data types

Function block FB_PDSql_My and data block DB_PDSql_My uses several custom PLC data
types to communicate with user application. The following is a list and description of these data
types used in the library for S7-1200 and S7-1500.

S7-1200 S7-1500

1.6.1 tConnection

This data type provides path parameters to connect to MySQL Server.

Name Data type Default value Comment


IP1 USInt 192 Octet 1 of the SQL Server‘s IP address

IP2 USInt 168 Octet 2 of the SQL Server‘s IP address

IP3 USInt 1 Octet 3 of the SQL Server‘s IP address

IP4 USInt 1 Octet 4 of the SQL Server‘s IP address


Remote port of the MySQL Server. MySQL default is
Port UDInt 3306
3306.
Reference to this connection. The parameter uniquely
ConnID CONN_OUC 1
identifies a connection within the PLC.
Hardware ethernet interface of the PLC used to the
HW HW_INTERFACE 64
communication. X1(64), X2(72)
Maximum time allowed to wait for connect, disconnect
Timeout Time T#2s and execute query command. After this time elapsed,
the error status is triggered.

7
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

1.6.2 tCmd

This data type provides command triggers to the Function block.

Name Data type Default value Comment


Connect 1 Bool False Trigger to connect to MySQL server
Disconnect 1 Bool False Trigger to disconnect from MySQL server
Trigger to execute MySQL query. If the MySQL
server has not been connected before activating
this signal, the connection process will start
automatically and then the MySQL command will
ExecuteQuery 1 Bool False
be executed after a successful connection to the
sql server. So the user application control can
use only this one trigger signal to connect and
execute commands automatically.

1 The bit signal is active in TRUE and triggered on the rising edge. Function block always resets this
bit.

1.6.3 tLogin

This data type provides login parameters to the SQL Server.

Name Data type Default value Comment


HostName String '' Freely definable name (can be empty)
UserName String '' The user name of MySQL Server account
Password String '' The password of MySQL Server account

Database String '' The name of database (not used / can be empty)

8
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

1.6.4 tStat

This data type provides status signals from the Function block.

Name Data type Default value Comment


Connected Bool False TRUE when logged to MySQL server successfully
Busy Bool False TRUE during execution of MySQL statement
TRUE when MySQL statement was executed
ExecutedOK Bool False
successfully
Error Bool False TRUE when MySQL statement ended with an error

Status Word W#16#0000 Info or error code


Number of affected rows after executing the last
AffectedRows UDInt 0
MySQL command
Info or error message after the last MySQL
Message String ''
command executed

1.6.5 tLicense

This data type provides runtime license key.

Name Data type Default value Comment


LicKey1 DWord 16#00 License Key part 1
LicKey2 DWord 16#00 License Key part 2
LicKey3 DWord 16#00 License Key part 3

LicKey4 DWord 16#00 License Key part 4

LicKey5 DWord 16#00 License Key part 5

1.6.6 tQuery

This data type provides MySQL query string.

Name Data type Default value Comment


Query Array[1..2] of String '' Array of MySQL Query

9
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

1.6.7 tRow_1200

This data type provides row data for sql table result (S7-1200 version).

Name Data type Default value Comment


Bool Bool False Bool value
Null Bool False NULL value
DInt DInt 0 DInt value

LReal LReal 0.0 LReal value

String String[50] '' String value

Datetime DTL DTL#1970-01-01-00:00:00 Datetime value

1.6.8 tRow_1500

This data type provides row data for sql table result (S7-1500 version).

Name Data type Default value Comment


Bool Bool False Bool value
Null Bool False NULL value
LInt LInt 0 LInt value

LReal LReal 0.0 LReal value

String String[50] '' String value

Datetime DTL DTL#1970-01-01-00:00:00 Datetime value

10
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

1.6.9 tColumn_1200

This data type provides column data for sql table result (S7-1200 version).
Default
Name Data type Comment
value
Name String '' Column’s name
CharacterSet UInt 0 Character set
ColumnLength UDint 0 Length

Type Byte 16#0 Data type

Flags Word 16#0 Specific flags

Decimals Byte 16#0 Number of decimal places

Rows Array[1..10] of "tRow_1200" Array of rows

1.6.10 tColumn_1500

This data type provides column data for sql table result (S7-1500 version).
Default
Name Data type Comment
value
Name String '' Column’s name
CharacterSet UInt 0 Character set
ColumnLength UDint 0 Length

Type Byte 16#0 Data type

Flags Word 16#0 Specific flags

Decimals Byte 16#0 Number of decimal places

Rows Array[1..10] of "tRow_1500" Array of rows

1.6.11 tSqlTable_1200

This data type provides main parameters and sql table result (S7-1200 version).

Default
Name Data type Comment
value
ColumnCount UInt 0 The number of readed columns
RowCount UInt 0 The number of readed rows
Maximum allowed number of columns.
MaxColumns UInt 10 The value must be less than or equal to
tSqlTable.Columns array size
Maximum allowed number of rows. The
MaxRows UInt 10 value must be less than or equal to
tColumn.Rows array size
Columns Array[1..10] of "tColumn_1200" Array of columns

11
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

1.6.12 tSqlTable_1500

This data type provides main parameters and sql table result (S7-1500 version).

Default
Name Data type Comment
value
ColumnCount UInt 0 The number of readed columns
RowCount UInt 0 The number of readed rows
Maximum allowed number of columns.
MaxColumns UInt 10 The value must be less than or equal to
tSqlTable.Columns array size
Maximum allowed number of rows.
MaxRows UInt 10 The value must be less than or equal to
tColumn.Rows array size
Columns Array[1..10] of "tColumn_1500" Array of columns

The MaxColumns parameter must be less than equal to the array size of SqlTable.Columns and
the MaxRows parameter must be less than equal to the array size of tColumn.Rows
otherwise the PLC may go to

1.7 Errors
Here is the list of errors written to the tStat.Status variable as output from the function block
FB_PDSql. During the error code, a more detailed description of the error is written to the
tStat.Message.

Table 1.3
Code Error message Tips
W#16#0000 No error
W#16#8001 Cannot connect to the remote server Check IP address and port
W#16#8002 Conection timeout Check IP address and port
W#16#8003 Login failed See details in tStat.Message
Something is wrong with ethernet
W#16#8004 Disconection timeout
connection
W#16#8005 MySQL Command execution timeout MySQL Server does not respond
W#16#8006 MySQL Packet error See details in tStat.Message
W#16#8007 MySQL Command execution error See details in tStat.Message
W#16#8080 Invalid license key Check the license key

12
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

1.8 License / Demo


The license key is binded to the PLC serial number. After creating a license key with an
online license tool on the webite www.plcdirectsql.com, these 5 license numbers need to be written
to LicKey1-5. It is recommended to write them as the start value so that they remain written after
the PLC restarts.
For the first 2 hours after switching on the power supply of the PLC device, the library is
fully functional even without a valid license key and works in demo mode.

13
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

2 Instalation

2.1 MySQL Community Server 8.0.19


This chapter describes how to install MySQL Community Server 8.0.19 and set it up for
communicating with the PDSql library. The link to download the instalation package is here ->
https://dev.mysql.com/downloads/mysql/

→ „Go to Download Page >“

→ „Download“

14
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

→ „No thanks, just start my download.“

The mysql-installer-community-8.0.19.0.msi will be downloaded in your download


directory. Run this file to continue.

Select „Full“ → „Next >“

15
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

→ „Next >“

→ „Next >“

16
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

Select High Availability mode (e.g. Standalone MySQL Server ...) → „Next >“

Check TCP/IP, select Port number (default 3306), check Open Windows Firewall ports
for network access → „Next >“

17
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

Select Use Legacy Authentication Method → „Next >“

Define MySQL Root Password → „Next >“

18
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

Select Windows service → „Next >“

→ „Execute“

19
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

→ „Next >“

→ „Finish“

20
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

2.1.1 Host access settings for your MySQL account

You will need to run MySQL Workbench to be able to connect and manage the MySQL
Server.

1. Select your MySQL instance

2. Enter password for root account

3. Connect to MySQL server

21
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

To access the MySQL server through a MySQL account from any host you must set up the
following:

1. Click → Users and Privileges


2. Select → User (e.g. root)
3. Change → Limit to Hosts Matching = „%“ (‚%‘ means access from any host)

22
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

2.2 Instalation PDSql Library My to TIA Portal

➢ Retrive PDSql Library My from archive.


23
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

➢ Open PDSql Library My in TIA Portal.

➢ Select PDSql Library My file and open.

24
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

Example for S7-1200 project

➢ Drag & Drop Function Block from library to your Project / Program blocks
This action will also automatically transfer all necessary data types from library to
Project / PLC data types

25
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

➢ Drag & Drop Functions from library to your Project / Program blocks

26
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

➢ Drag & Drop Master copies / S7-1200 data blocks from library to your Project / Program
blocks

27
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

3 Examples

In the following examples we will use

• or
• MySQL Server
• PDSql Library My.

3.1 Create a table


Using or , as first we will create a schema
“plcdb” and a table named “test_table” that will be used in the following examples and will contain
these columns ->

Column name Data type Description


[Id] INT auto increment Id number
[Datetime] DATETIME(4) date and time
[FruitName] VARCHAR(20) fruit name
[Quantity] INT(10) fruit quantity
[Weight] REAL fruit weight
[CitrusType] BIT(1) citrus type [1 – Yes, 0 – No]

➢ MySQL statement to create a schema

CREATE SCHEMA plcdb

➢ MySQL statement to create a table

CREATE TABLE plcdb.test_table (


`Id` INT NOT NULL AUTO_INCREMENT,
`Datetime` DATETIME(4) NULL,
`FruitName` VARCHAR(20) NULL,
`Quantity` INT(10) NULL,
`Weight` REAL NULL,
`CitrusType` BIT(1) NULL,
PRIMARY KEY (`Id`));

➢ Insert test rows into the table

INSERT INTO plcdb.test_table (Datetime,FruitName,Quantity,Weight,CitrusType)


VALUES ('2019-07-01 23:42:18','Apple',10,2.54,0),
('2019-07-01 23:48:54','Orange',16,3.28,1),
('2019-07-01 23:52:26','Lemon',8,1.64,1),
('2019-07-01 23:56:33','Mango',7,4.32,0)

28
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

Table: „test_table“

3.2 SELECT row from table

➢ Read a row from the table where FruitName = "Apple"

Syntax of the SQL statement in or

SELECT * FROM plcdb.test_table WHERE FruitName = 'Apple'

Syntax of the SQL statement in TIA environment

Query[1] := 'SELECT * FROM plcdb.test_table WHERE FruitName=$'Apple$'';


Query[2] := '';

In the TIA environment you must add a dollar ($) sign before each single quote in
the query string !

Execute MySQL query with PDSql Library My

1. Execute MySQL query


Cmd.ExecuteQuery := TRUE;
Set Cmd.ExecuteQuery on TRUE will also automatically connect to MySQL server
if not connected yet.

2. Wait for the execution of the MySQL query


(Stat.ExecutedOK OR Stat.Error) = TRUE;

29
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

3. If ExecutedOK then result is stored in the SQLtable

The status after


SELECT command

The number of rows and


columns returned after
SELECT command

Id = 1

Datetime =
“2019-07-01-23:42:18”

FruitName = “Apple”

30
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

Quantity = 10

Weight = 2.54

CitrusType = FALSE

4. If Error then Stat.Status contains an error code and


Stat.Message contains an error message

31
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

3.3 INSERT row to the table

➢ Write a new row into the table

Syntax of the MySQL statement in or

INSERT INTO plcdb.test_table (Datetime,FruitName,Quantity,Weight,CitrusType) VALUES


('2019-07-02 10:42:56','Banana',24,8.48,0)

Syntax of the MySQL statement in TIA environment

Query[1] := 'INSERT INTO plcdb.test_table (Datetime,FruitName,Quantity,Weight,CitrusType)


VALUES ($'2019-07-02 10:42:56$',$'Banana$',24,8.48,0)';
Query[2] := '';

In the TIA environment you must add a dollar ($) sign before each single quote
in the query string !

Compose INSERT string from input variables (SCL language)

#dtlDateTime := '2019-07-02 10:42:56';


#sFruitName := 'Banana';
#iQuantity := 24;
#rWeight := 8.48;
#bCitrusType := FALSE;

#str := 'INSERT INTO plcdb.test_table (Datetime,FruitName,Quantity,Weight,CitrusType) VALUES($'';


#str := CONCAT(IN1 := #str, IN2 := "fcDTLString"(#dtlDateTime));
#str := CONCAT(IN1 := #str, IN2 := '$',$'');
#str := CONCAT(IN1 := #str, IN2 := #sFruitName);
#str := CONCAT(IN1 := #str, IN2 := '$',');
#str := CONCAT(IN1 := #str, IN2 := INT_TO_STRING(#iQuantity));
#str := CONCAT(IN1 := #str, IN2 := ',');
#str := CONCAT(IN1 := #str, IN2 := REAL_TO_STRING(#rWeight));
#str := CONCAT(IN1 := #str, IN2 := ',');
#str := CONCAT(IN1 := #str, IN2 := INT_TO_STRING(BOOL_TO_BYTE(#bCitrusType)));
#str := CONCAT(IN1 := #str, IN2 := ')');

"DB_PDSql_1200".Query.Query[1] := #str;
"DB_PDSql_1200".Query.Query[2] := '';

32
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

Execute MySQL query with PDSql Library My

1. Execute MySQL query


Cmd.ExecuteQuery := TRUE;
Set Cmd.ExecuteQuery on TRUE will also automatically connect to MySQL server if not
connected yet.

2. Wait for the execution of the MySQL query


(Stat.ExecutedOK OR Stat.Error) = TRUE;

3. If ExecutedOK then a row was inserted successfully

“test_table” after executing the INSERT command

4. If Error then Stat.Status contains an error code and


Stat.Message contains an error message

33
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

3.4 UPDATE row in table

➢ Update an existing row in the table where FruitName = “Mango”

In this example we will update a record without specifying a key in the where
clause, so we need to set SQL_SAFE_UPDATES = 0

Syntax of the MySQL statement in or

SET SQL_SAFE_UPDATES = 0;

UPDATE plcdb.test_table SET Quantity = 32, Weight = 12.68 WHERE FruitName = 'Mango'

Syntax of the MySQL statement in TIA environment

Query[1] := UPDATE plcdb.test_table SET Quantity=32, Weight=12.68 WHERE FruitName =


$'Mango$'';
Query[2] := '';

In the TIA environment you must add a dollar ($) sign before each single quote
in the query string !

Compose UPDATE string from input variables (SCL language)

#sFruitName := 'Mango';
#iQuantity := 32;
#rWeight := 12.68;

#str := 'UPDATE plcdb.test_table SET Quantity=';


#str := CONCAT(IN1 := #str, IN2 := INT_TO_STRING(#iQuantity));
#str := CONCAT(IN1 := #str, IN2 := ',Weight=');
#str := CONCAT(IN1 := #str, IN2 := REAL_TO_STRING(#rWeight));
#str := CONCAT(IN1 := #str, IN2 := ' WHERE FruitName=$'');
#str := CONCAT(IN1 := #str, IN2 := #sFruitName);
#str := CONCAT(IN1 := #str, IN2 := '$'');

"DB_PDSql_1200".Query.Query[1] := #str;
"DB_PDSql_1200".Query.Query[2] := '';

34
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

Execute MySQL query with PDSql Library My

1. Execute MySQL query


Cmd.ExecuteQuery := TRUE;
Set Cmd.ExecuteQuery on TRUE will also automatically connect to MySQL server if not
connected yet.

2. Wait for the execution of the MySQL query


(Stat.ExecutedOK OR Stat.Error) = TRUE;

3. If ExecutedOK then a row was updated successfully

“test_table” after executing the UPDATE command

4. If Error then Stat.Status contains an error code and


Stat.Message contains an error message

35
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

3.5 DELETE row from table

➢ Delete an existing row from the table where FruitName = “Lemon”

In this example we will delete a record without specifying a key in the where
clause, so we need to set SQL_SAFE_UPDATES = 0

Syntax of the MySQL statement in or

SET SQL_SAFE_UPDATES = 0

DELETE FROM plcdb.test_table WHERE FruitName = 'Lemon'

Syntax of the MySQL statement in TIA environment

Query[1] := 'DELETE FROM plcdb.test_table WHERE FruitName=$'Lemon$'';


Query[2] := '';

In the TIA environment you must add a dollar ($) sign before each single quote
in the query string !

Compose DELETE string from input variables (SCL language)

#sFruitName := 'Lemon';

#str := 'DELETE FROM plcdb.test_table WHERE FruitName=$'';


#str := CONCAT(IN1 := #str, IN2 := #sFruitName);
#str := CONCAT(IN1 := #str, IN2 := '$'');

"DB_PDSql_1200".Query.Query[1] := #str;
"DB_PDSql_1200".Query.Query[2] := '';

Execute MySQL query with PDSql Library My

1. Execute MySQL query


Cmd.ExecuteQuery := TRUE;
Set Cmd.ExecuteQuery on TRUE will also automatically connect to MySQL server if not
connected yet.

36
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)

2. Wait for the execution of the MySQL query


(Stat.ExecutedOK OR Stat.Error) = TRUE;

3. If ExecutedOK then the existing row was deleted successfully

“TestTable” after executing the DELETE command

4. If Error then Stat.Status contains an error code and


Stat.Message contains an error message

37
PDSql Library My v1.3.4 www.plcdirectsql.com

You might also like