String-Größen und lange SQL-Befehle
Standard-Einschränkungen
Die MySQL-Library arbeitet mit Arrays zur Erstellung von SQL-Befehlen
Strings haben eine feste Maximallänge. Durch die Array-Aufteilung können längere Befehle erstellt werden.
Globale Konstanten anpassen
Diese Werte können Sie in den Global Constants ändern:
Wo finde ich die Einstellungen?
// In den GLOBAL CONSTANTS der Library:
gc_MySQL_iStatementMax : INT := 10; // Array-Größe (0..10 = 11 Elemente)
gc_MySQL_iStatementLength : INT := 100; // Zeichen pro ElementBeispiel: Größere Befehle ermöglichen
// Standard (Default):
gc_MySQL_iStatementMax := 10; // 11 Teile
gc_MySQL_iStatementLength := 100; // 100 Zeichen/Teil
// Erweitert für längere Befehle:
gc_MySQL_iStatementMax := 20; // 21 Teile
gc_MySQL_iStatementLength := 200; // 200 Zeichen/Teil
Lange SQL Befehle
Strategie 1: Befehl aufteilen (Standard)
Beispiel: Langer INSERT mit vielen Werten
VAR
arrLongCmd : ARRAY[0..10] OF STRING(100);
END_VAR
// Teil 1: INSERT-Anfang
arrLongCmd[0] := 'INSERT INTO machine_data (id, timestamp, temp, pressure, speed, status, operator) ';
// Teil 2: VALUES-Anfang
arrLongCmd[1] := 'VALUES (';
// Teil 3-8: Einzelne Werte
arrLongCmd[2] := CONCAT(INT_TO_STRING(iMachineID), ', ');
arrLongCmd[3] := '$272024-01-15 10:30:00$27, ';
arrLongCmd[4] := CONCAT(REAL_TO_STRING(rTemp), ', ');
arrLongCmd[5] := CONCAT(REAL_TO_STRING(rPressure), ', ');
arrLongCmd[6] := CONCAT(REAL_TO_STRING(rSpeed), ', $27');
arrLongCmd[7] := CONCAT(sStatus, '$27, $27');
arrLongCmd[8] := CONCAT(sOperator, '$27)');
// Rest leer lassen
arrLongCmd[9] := '';
arrLongCmd[10] := '';Wichtig: Jedes Element darf maximal 100 Zeichen haben!
Strategie 2: Zwischenvariablen verwenden
Wenn ein Teil-String zu lang wird:
VAR
sValuesPart1 : STRING(100);
sValuesPart2 : STRING(100);
END_VAR
// Werte in Zwischenvariablen zusammenbauen
sValuesPart1 := CONCAT(INT_TO_STRING(iValue1), ', ');
sValuesPart1 := CONCAT(sValuesPart1, INT_TO_STRING(iValue2));
sValuesPart1 := CONCAT(sValuesPart1, ', ');
sValuesPart1 := CONCAT(sValuesPart1, INT_TO_STRING(iValue3));
sValuesPart2 := CONCAT(', ', REAL_TO_STRING(rValue4));
sValuesPart2 := CONCAT(sValuesPart2, ', ');
sValuesPart2 := CONCAT(sValuesPart2, REAL_TO_STRING(rValue5));
// Dann in Array einfügen
arrLongCmd[0] := 'INSERT INTO data (v1, v2, v3, v4, v5) VALUES (';
arrLongCmd[1] := sValuesPart1;
arrLongCmd[2] := sValuesPart2;
arrLongCmd[3] := ')';Praktische Beispiele
Beispiel 1: INSERT mit vielen Spalten
Aufgabe: 15 Werte in Datenbank schreiben
VAR
// Daten
iID : INT := 1;
sName : STRING(50) := 'Maschine A';
rTemp1 : REAL := 23.5;
rTemp2 : REAL := 24.1;
rTemp3 : REAL := 22.8;
rPressure1 : REAL := 1.5;
rPressure2 : REAL := 1.6;
rSpeed1 : REAL := 1500.0;
rSpeed2 : REAL := 1520.0;
sOperator : STRING(30) := 'Schmidt';
sShift : STRING(10) := 'Frühschicht';
iCounter : INT := 1234;
xAlarm : BOOL := FALSE;
sStatus : STRING(20) := 'RUNNING';
sLocation : STRING(30) := 'Halle 3';
// SQL-Befehl
arrInsertCmd : ARRAY[0..10] OF STRING(100);
END_VAR
// INSERT aufbauen in kleinen Teilen
arrInsertCmd[0] := 'INSERT INTO machines (id, name, temp1, temp2, temp3, pressure1, pressure2) ';
arrInsertCmd[1] := 'VALUES (';
arrInsertCmd[2] := CONCAT(INT_TO_STRING(iID), ', $27');
arrInsertCmd[3] := CONCAT(sName, '$27, ');
arrInsertCmd[4] := CONCAT(REAL_TO_STRING(rTemp1), ', ');
arrInsertCmd[5] := CONCAT(REAL_TO_STRING(rTemp2), ', ');
arrInsertCmd[6] := CONCAT(REAL_TO_STRING(rTemp3), ', ');
arrInsertCmd[7] := CONCAT(REAL_TO_STRING(rPressure1), ', ');
arrInsertCmd[8] := CONCAT(REAL_TO_STRING(rPressure2), ')');Aber: Nur 7 von 15 Werten passen! Lösung: Zweiter INSERT oder UPDATE
// Zweiter Teil als UPDATE
arrUpdateCmd[0] := 'UPDATE machines SET speed1 = ';
arrUpdateCmd[1] := CONCAT(REAL_TO_STRING(rSpeed1), ', speed2 = ');
arrUpdateCmd[2] := CONCAT(REAL_TO_STRING(rSpeed2), ', operator = $27');
arrUpdateCmd[3] := CONCAT(sOperator, '$27, shift = $27');
arrUpdateCmd[4] := CONCAT(sShift, '$27, counter = ');
arrUpdateCmd[5] := CONCAT(INT_TO_STRING(iCounter), ', status = $27');
arrUpdateCmd[6] := CONCAT(sStatus, '$27 WHERE id = ');
arrUpdateCmd[7] := INT_TO_STRING(iID);Beispiel 2: SELECT mit langer WHERE-Bedingung
VAR
dtStart : STRING := '2024-01-01';
dtEnd : STRING := '2024-12-31';
rMinTemp: REAL := 20.0;
rMaxTemp: REAL := 30.0;
iMachID : INT := 1;
END_VAR
// Lange WHERE-Bedingung aufteilen
arrSelectCmd[0] := 'SELECT * FROM measurements WHERE ';
arrSelectCmd[1] := 'machine_id = ';
arrSelectCmd[2] := CONCAT(INT_TO_STRING(iMachID), ' AND ');
arrSelectCmd[3] := 'timestamp BETWEEN $27';
arrSelectCmd[4] := CONCAT(dtStart, '$27 AND $27');
arrSelectCmd[5] := CONCAT(dtEnd, '$27 AND ');
arrSelectCmd[6] := 'temperature BETWEEN ';
arrSelectCmd[7] := CONCAT(REAL_TO_STRING(rMinTemp), ' AND ');
arrSelectCmd[8] := REAL_TO_STRING(rMaxTemp);Beispiel 3: UPDATE mit vielen Feldern
// Problem: 10 Felder aktualisieren
// Lösung: In sinnvolle Teile aufteilen
arrUpdateCmd[0] := 'UPDATE production SET ';
arrUpdateCmd[1] := 'temp1=' + REAL_TO_STRING(rTemp1) + ', ';
arrUpdateCmd[2] := 'temp2=' + REAL_TO_STRING(rTemp2) + ', ';
arrUpdateCmd[3] := 'temp3=' + REAL_TO_STRING(rTemp3) + ', ';
arrUpdateCmd[4] := 'pressure1=' + REAL_TO_STRING(rPress1) + ', ';
arrUpdateCmd[5] := 'pressure2=' + REAL_TO_STRING(rPress2) + ', ';
arrUpdateCmd[6] := 'speed=' + REAL_TO_STRING(rSpeed) + ', ';
arrUpdateCmd[7] := 'status=$27' + sStatus + '$27 ';
arrUpdateCmd[8] := 'WHERE id = ' + INT_TO_STRING(iID);Zuletzt aktualisiert

