Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am using node-red and trying to extract the text from the image using OCR and I want to send the text to SQL, Below is my basic solution doing it using function node.
company0 = msg.payload[0];
amount1= msg.payload[1];
purchase2 = msg.payload[2];
invoice3 = msg.payload[3];

data ="insert into ocrtest (company0,amount1,purchase2,invoice3) values("+company0+","+amount1+","+purchase2+","+invoice3+")"

return{topic : data}

Here is the error result...

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '25, 2020

)' at line 7


What I have tried:

I am not really so familiar in programming. I just tried using other nodes but failed.
Posted
Updated 20-Jun-22 22:10pm
v2

The problem is probably the actual data: you don't delimit any of the values with SQL string indicators, so SQL interpret them as instructions and if any of them contain a space or quote your get errors.

But don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Exactly how you do that will depend on your chosen language and framework - "linux" is an OS, not a language, so we have no idea what you are using - there are many languages that could fit your code!
Google will find you instructions.
 
Share this answer
 
Try to enclose values like this:
,'${invoice3}')"
And of course try to use parameterized queries as Griff said.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900