Is there some string escaping or preparing?
See original GitHub issueHello. I’m using node-postgres. And I want to insert some strings into some table. If I’m using single query, I can do like this and everything is working ok:
client.query('INSERT INTO sometbl VALUES ($1, $2)', ['foo', 'bar']);
But since I am using transactions and trying to do like this:
client.query('BEGIN; INSERT INTO sometbl VALUES ($1, $2); UPDATE sometable SET somevalue = $3; END;', ['foo', 'bar', 'foobar']);
I am getting an error with code 42601: 'cannot insert multiple commands into a prepared statement'
.
Yes, I know that I can use simple string concatenation or some realizations of sprintf() to insert these parameters into the query string, but, I think, it isn’t safe to do it without any preparation like escaping. Is there some convinient way to do such preparation before inserting string values into the query string?
For example, I have found PQescapeLiteral function in the libpq, maybe there is some interface to this or similar function exists? If no, can it be implemented? //Sorry if the question is silly, I’m newbie in postgresql and node.
Issue Analytics
- State:
- Created 12 years ago
- Reactions:3
- Comments:9 (4 by maintainers)
Top GitHub Comments
(NOTE: the current
pg
supports bothescapeIdentifier
andescapeLiteral
on client instances: https://github.com/brianc/node-postgres/blob/3f6760c62ee2a901d374b5e50c2f025b7d550315/packages/pg/lib/client.js#L408-L437 )Sure; escapeIdentifier is for escaping identifiers as documented here: https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS “(To include a double quote, write two double quotes.)”
escapeLiteral is for escaping string constants as documented here: https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS