Trying to write to mariaDB with mysql node

Ok, so I have a function node to update a maria DB.

This function node will deploy and retrieve from the DB:

msg.topic = 'SELECT * FROM dwell_time_alerts LIMIT 1';

node.warn(msg.topic); // Log the query to Node-RED console

return msg;

However, this query fails to even deploy w/ a no response from server error:

let cam_name = msg.payload.camera?.settings?.name || "Unknown";
let ip_add = msg.payload.camera?.settings?.ip || "0.0.0.0";
let mac_add = msg.payload.camera?.settings?.mac || "Unknown";
let area = msg.payload.camera?.settings?.area || "Unknown";
let timestamp = msg.payload.camera?.settings?.timestamp || new Date().toISOString();
let url = msg.payload.camera?.settings?.url || "";

msg.topic = `INSERT INTO dwell_time_alerts (camera_name, ip_add, mac_add, area, timestamp, url) 
            VALUES ('${cam_name}', '${ip_add}', '${mac_add}', '${area}', '${timestamp}', '${url}')`;

node.warn(msg.topic); // Log the query to Node-RED console

return msg;

I'm at a loss.

Tried updating my query to this:

let cam_name = msg.payload.camera?.settings?.name || "Unknown";
let ip_add = msg.payload.camera?.settings?.ip || "0.0.0.0";
let mac_add = msg.payload.camera?.settings?.mac || "Unknown";
let area = msg.payload.camera?.settings?.area || "Unknown";
let timestamp = msg.payload.camera?.settings?.timestamp || new Date().toISOString();
let url = msg.payload.camera?.settings?.url || "";

msg.topic = "INSERT INTO 'nodered'.'dwell_time_alerts' ('camera_name', 'ip_add', 'mac_add', 'area', 'timestamp', 'url') VALUES ('"+ cam_name+"', '"+ ip_add+"', '"+ mac_add+"', '"+ area+"', '"+ timestamp+"', '"+ url+"')";

node.warn(msg.topic); // Log the query to Node-RED console

return msg;

Same message:

Deploy failed: no response from server.

Welcome to the forum @djackson-telaid .

Deploy failed: no response from server does not indicate any problem with your function code.
It indicates a communication problem between the browser and Node-red. Perhaps Node-red is no longer running, perhaps it's a network issue.

Can you give us the details of what computer Node-red is installed on and what computer is running the browser?
What URL did you use to access the editor?

That said, I do think there are issues with your code.

In the first version you seem to be mixing up this syntax for common-or-garden variables and that for environment variables (I rarely use environment variables so I'm not certain)

In the second you are hard coding the values into msg.topic. It's legal in MySQL but bad form because this kind of INSERT is less secure than a "prepared query", which I think your first attempt is supposed to do.

I find using a "template" node helps simplify constructing the MySQL query.

Make sure you set the property in the node to... msg.topic


This is what the query statement looks like... (note: In this example all the items are 'strings')
If any of the items are numeric, then leave-off the leading and trailing primes as below...

{{payload.sp_max}}, 
{{payload.temperature}}, 
{{payload.sp_min}},
INSERT INTO mqtt_summary (uniqueID, date, time, ip, location) VALUES (
'{{uniqueID}}', 
'{{date}}', 
'{{time}}', 
'{{ip}}', 
'{{location}}'
)

It's running in a Docker container, and I've verified it's operational, as when I remove the INSERT code, it's able to deploy just fine. Again, I can pull data from the maria DB.

Oh OK. Docker is a foreign country. They do things differently there.

Look in the node red log and see what it shows when you deploy. I don't know how to do that in docker but hopefully you do, since you are using it.

Edit: in fact first disconnect the DB node input and instead feed the function node into a debug node set to output complete message and deploy that. Assuming it deploys, check the message is as you expect.

Ignore the syntax of the queries for a moment, as they don't matter currently. The problem is this builds and deploys:

let ip_add = msg.payload.camera?.settings?.ip || "0.0.0.0";
let mac_add = msg.payload.camera?.settings?.mac || "Unknown";
let area = msg.payload.camera?.settings?.area || "Unknown";
let timestamp = msg.payload.camera?.settings?.timestamp || new Date().toISOString();
let url = msg.payload.camera?.settings?.url || "";

msg.topic = 'INSERT * dwell_time_alerts LIMIT 1';

node.warn(msg.topic); // Log the query to Node-RED console

return msg;

This does NOT:

let cam_name = msg.payload.camera?.settings?.name || "Unknown";
let ip_add = msg.payload.camera?.settings?.ip || "0.0.0.0";
let mac_add = msg.payload.camera?.settings?.mac || "Unknown";
let area = msg.payload.camera?.settings?.area || "Unknown";
let timestamp = msg.payload.camera?.settings?.timestamp || new Date().toISOString();
let url = msg.payload.camera?.settings?.url || "";

msg.topic = 'INSERT INTO dwell_time_alerts LIMIT 1';

node.warn(msg.topic); // Log the query to Node-RED console

return msg;

The word INTO is causing it to not build/deploy. How can that be?

Believe it or not, this builds:

let cam_name = msg.payload.camera?.settings?.name || "Unknown";
let ip_add = msg.payload.camera?.settings?.ip || "0.0.0.0";
let mac_add = msg.payload.camera?.settings?.mac || "Unknown";
let area = msg.payload.camera?.settings?.area || "Unknown";
let timestamp = msg.payload.camera?.settings?.timestamp || new Date().toISOString();
let url = msg.payload.camera?.settings?.url || "";

let insertKeyword = "INTO";
msg.topic = `INSERT ${insertKeyword} dwell_time_alerts`;

node.warn(msg.topic); // Log the query to Node-RED console

return msg;

go figure...

It can't be - INTO is part of a string literal.

Now Node-red is not going to be validating SQL syntax in a string at deploy (or any other) time but what's going on with LIMIT1 at the end of the INSERT statement? Is that really valid syntax?

image
image

These use different single quote characters. Why?

Are those with the db node input disconnected as I suggested?

That is so that the variable insertKeyword is inserted into the string. The backticks are for Template String Interpolation.

I think it is more likely the LIMIT 1 on an insert query that is causing the db node to become confused.

I did think of that but since I'm not familiar with it, I didn't check if it's ok for backticks to replace single or double quotes.

But surely that's a runtime issue not at deploy?

If the db node is hanging soon after deploy then it might look as if the Deploy is not completing. That is why I have asked @djackson-telaid to try it with the db node not connected, and also to show the log while trying to deploy, which he has also not responded to.

@djackson-telaid Can you post an example of the msg.payload you are feeding into the function? (Use the copy value button in the debug sidebar, not a screenshot)

Also the exact SQL query syntax that your first posted function node is intended to generate from it.

So it turns out, it was "deploying" the changes all along, but reporting no response from the server. After rebooting the machine and hard-refreshing the screen, the changes took regardless. Not sure why it's doing it, but thanks for all your help.

That is what we have been trying to tell you all along.