Establish first connection to the database
Establish first connection to the database
Prerequisites
1
Declare variables
VAR
// Connection parameters
sHost : STRING := '192.168.1.100'; // IP address of your MySQL server
uiPort : UINT := 3306; // Default MySQL port
sDatabase : STRING := 'testdb'; // Name of your database
sUsername : STRING := 'root'; // Your username
sPassword : STRING := 'MeinPasswort123'; // Your password
// Control
xDoOpen : BOOL := FALSE; // Trigger to open
xConnected : BOOL := FALSE; // Status: connected
// Function block
MySQL_ConnString : MySQL_ConnectionString; // Connection object
fbMySQL_Open : MySQL_Open; // Function block
// Error handling
eError : ERROR; // Error code
sExecuteState : STRING(200); // Status message
END_VAR2
Open connection - code
// Open connection when xDoOpen is TRUE
IF xDoOpen THEN
fbMySQL_Open(
sHost := '192.168.1.100', // CHANGE: your server IP
uiPort := 3306,
sDatabase := 'testdb', // CHANGE: your database name
sUsername := 'root', // CHANGE: your username
sPassword := 'MeinPasswort123', // CHANGE: your password
xStart := xDoOpen,
MySQL_Connection:= MySQL_ConnString,
xConnected => xConnected,
eError => eError,
sExeute_State => sExecuteState
);
END_IFImportant values to adjust
Parameter
Example value
Where do I find the value?
Troubleshooting
Error
Cause
Solution
Last updated

