New to node-red and have been fighting with this for a while now and I seem to be quite stuck...
Using mariaDB with SQL node. Sending data from form to database
The fist part is working fine
This is my whole function block:
msg.quantity = msg.payload.bt1;
msg.topic = "INSERT INTO `ORDERS` (`OrderID`, `CreatedAt`, `FinishedAt`, `Status`) VALUES (NULL, current_timestamp(), '0000-00-00 00:00:00.000000', '1'); INSERT INTO `QUANTITIES` (`QuantityID`, `ProductType`, `Quantity`, `OrderID`) VALUES (NULL, '1', "+ msg.quantity +",(SELECT OrderID FROM ORDERS ORDER BY OrderID DESC LIMIT 1)); DROP PROCEDURE IF EXISTS dowhile;DELIMITER $$ CREATE PROCEDURE dowhile() BEGIN DECLARE num_rows INT DEFAULT (SELECT Quantity FROM QUANTITIES ORDER BY OrderID DESC LIMIT 1); WHILE num_rows > 0 DO INSERT INTO `BARCODES`(`BarCode`, `OrderID`, `ProductType`) VALUES (NULL, (SELECT OrderID FROM ORDERS ORDER BY OrderID DESC LIMIT 1), '1'); SET num_rows = num_rows - 1; END WHILE; END$$ call dowhile();";
return msg;
This is the part giving trouble, but it doesn't give any errors when trying to query it with phpMyadmin and creates the correct rows.:
DELIMITER $$ CREATE PROCEDURE dowhile() BEGIN DECLARE num_rows INT DEFAULT (SELECT Quantity FROM QUANTITIES ORDER BY OrderID DESC LIMIT 1); WHILE num_rows > 0 DO INSERT INTOBARCODES(BarCode,OrderID,ProductType) VALUES (NULL, (SELECT OrderID FROM ORDERS ORDER BY OrderID DESC LIMIT 1), '1'); SET num_rows = num_rows - 1; END WHILE; END$$ call dowhile();"
Why are you dropping & recreating the procedure each time? The purpose of a procedure is to enable the db engine to handle some incoming data more efficiently by compiling the procedure, if you keep recreating it, it can't do that.
Also I suggest splitting it into separate messages, one for each SQL command. Then you will be able more clearly to see whether the problem is the end of one statement or the start of the next.
All the stored procedure does is to create a compiled function internal to the db engine that lets you slot data in more efficiently. If you are sending lots of updates of the same structure, procedures are a good bet. But you only create the procedure once - the overheads for creating it are relatively high.