Can not send data to table MariaDB, please help

I try to make sense how to connect with a MariaDB.

I use the following node:

I do have connection with my database.
I've created the table in the database.

My function node to inject data in database node:

client = 'client1'
project = 'project1'
meter_id = 'meter_id1'
date = msg.payload;
value = 1
msg.topic = "INSERT INTO 'meterdata' ( 'client', 'project', 'meter_id', 'date', 'value') VALUES ( "+ client+", "+ project +", "+ meter_id +", "+ date +", "+ value +");"
return msg;

After I inject I get the following message:
Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''meterdata' ( 'client', 'project', 'meter_id', 'date', 'value') VALUES ( clie...' at line 1

Can someone point me in the right direction?

Start by using a sql statement that works from another client, such as the mysql command line (I don't know whether that can be used with mariadb, if not then whatever the equivalent is). Then at least you know you have a valid query.

However the thing that jumps out at me is that you don't seem to have the right sort of ticks around the database name. Looking at https://www.guru99.com/insert-into.html it seems that you need backticks not single quotes.

If you are using Windows 10? You can use HeidiSQL client application with mariaDB. It has a very friendly interface for Windows 10. It has a nice query editor, that provides details feedback on SQL query syntax errors.

As others have noted, you have a number of errors in your SQL query syntax. Strongly suggest you use a SQL query syntax reference and correct your query.

1 Like

You need to insert strings with single quotes.

client = 'client1'
project = 'project1'
meter_id = 'meter_id1'
date = msg.payload;
value = 1
msg.topic = "INSERT INTO 'meterdata' ( 'client', 'project', 'meter_id', 'date', 'value') VALUES ( '"+ client+"', '"+ project +"', '"+ meter_id +"', "+ date +", "+ value +");"
return msg;

Thank you.

I've found out that the following is working (function node into node-red-node-mysql node):

client = 'client1'
project = 'project1'
meter_id = 'meter_id1'
date = msg.payload;
value = 1
msg.topic = "INSERT INTO `digitalempire`.`meterdata` (`client`, `project`, `meter_id`, `date`, `value`) VALUES ('client1, 'project1', 'meter1', '2020-08-09 09:03:55', '10');"
return msg;

Now I only need to figure out how to implement the placeholders.

Using a template node is one way to avoid the injection risk scenario. I have seen a thread in the forum on this, just have not found it yet.

1 Like

The following is working (function node that pushes data into node-red-node-mysql node):

var client = "client5";
var project = "project5";
var meter_id = "meter_id5";
var year = 2020;
var month = 09;
var day = 09;
var hour = 09;
var minutes = 03;
var seconds = 56;
var date = year + "-"+ month +"-"+ day +" "+ hour +":"+ minutes +":"+ seconds;
var value = 5;
msg.topic = "INSERT INTO `digitalempire`.`meterdata` (`client`, `project`, `meter_id`, `date`, `value`) VALUES ('"+ client +"', '"+ project +"', '"+ meter_id +"', '"+ date +"', '"+ value +"');";
return msg;

The only thing what's odd is that the function node say it has an error. But the function node does work and the data appears in the database. If I have a fix within 2 months I will send an update.

Thanks again Nodi.Rbrum. You gave me the push in the right direction.

What is the error (hover over the error indication) and which line is it?

Hi Colin,

That is the strange thing, it does say it has an error but when I open the node it doesn't give any error on any line of code? When I hover on the error indicator on the node it gives the message:

Invalid properties:

  • noerrr

Export that node (select it then Export to clipboard in the hamburger menu) and paste it here using the </> button.

Hi Colin,

[{"id":"e726cd44.7e1e4","type":"function","z":"56c8b957.4603b8","name":"MySQL Function","func":"var client = \"client5\";\nvar project = \"project5\";\nvar meter_id = \"meter_id5\";\nvar year = 2020;\nvar month = 09;\nvar day = 09;\nvar hour = 09;\nvar minutes = 03;\nvar seconds = 56;\nvar date = year + \"-\"+ month +\"-\"+ day +\" \"+ hour +\":\"+ minutes +\":\"+ seconds;\nvar value = 5;\nmsg.topic = \"INSERT INTO `digitalempire`.`meterdata` (`client`, `project`, `meter_id`, `date`, `value`) VALUES ('\"+ client +\"', '\"+ project +\"', '\"+ meter_id +\"', '\"+ date +\"', '\"+ value +\"');\";\nreturn msg;","outputs":1,"noerr":1,"initialize":"","finalize":"","x":650,"y":320,"wires":[["63b05a97.8dfac4"]]}]

The leading 0's in month, day, hour and minute is causing it.

Yes, I just worked that out. Is it not legal javascript to have a leading 0 on a number?

Found this in stack overflow

A leading 0 indicates an octal number in JavaScript. An octal number cannot contain an 8; therefore, that number is invalid. Moreover, JSON doesn't (officially) support octal numbers, so formally the JSON is invalid, even if the number would not contain an 8. Some parsers do support it though, which may lead to some confusion. Other parsers will recognize it as an invalid sequence and will throw an error, although the exact explanation they give may differ.

since the assignment is
var month = 09;
and 9 is not an octal number, I'd guess that is why it was flagged

I'm going to open a seperate thread about just this issue.

1 Like

How did you found out? Just asking out of interest.

I imported your function and then commented out all the lines, found it was ok, so uncommented lines till the problem occurred.

The thread about the octal issue is Function node flags octal assignments with leading 0's, warns at deploy but runs fine

Ok. Thank you very much for your (zenofmud en colin) effort.

@WesleyFranken the solution is to remove all the leading zeros from the numbers.

However, what are you trying to achieve with the date? I presume what you are doing here is not the final solution. If you want to write the current date/time then you can specify in the database table design that you want the current date/time to be written automatically when you insert a record, then you don't have to do anything. Alternatively I would have expected that the database would expect an ISO format string for writing a timestamp rather than the string you are providing.

I try to export meterdata to a MariaDB so I can look to this energie data later. This data needs to be collected beacause of the EED and the controllers used in location are Unipi Axon PLC with Node-RED.

I was asking what you are trying to do with the date (not the data).