3

I want to fetch the rows from a postgres table where name = SUPREME INT'L, Note: this string has a single quote in between the name characters. I am using TypeORM as an ORM, POSTGRESQL as the database.

My query:

 import { getConnection } from 'typeorm';
 const connection =  getConnection();

 var query = `SELECT * from skusimulations where "name"= ? `;
 const output =await connection.query(query, ['SUPREME INT'L']) 

I am getting error while executing this, I want to escape the single quote by using stored proc.

Any help would be highly appreciated.

2
  • What error? Are you referring to your failure to escape the single quote in your literal, because the query itself looks like it is already using parameter escaping. Commented Feb 14, 2019 at 7:57
  • @RichardHuxton, yes indeed I used stored proc to escape the single quote. Btw I solved it by storing the "name" in a variable and passing the variable in the replacement array, another modification was instead of '?' changed it to $1. Commented Feb 14, 2019 at 8:22

2 Answers 2

11

I changed a few things by referring to the typeorm.io docs.

Final changes:

  var name = "SUPREME INT'L" ;
  var query = `SELECT * from skusimulations where "skuId"= $1 `;
  var skuData =await connection.query(query, [name])
Sign up to request clarification or add additional context in comments.

1 Comment

this was really helpful, thanks! , if someone need this for mssql (like me) you just need to change the $ for @ and the position parameter start with 0
1

for mysql await getEntityManager().query('SELECT * FROM tbl_1 WHERE name = ?', [ p_name ])

for mssql await getEntityManager().query('SELECT * FROM tbl_1 WHERE name = @0', [ p_name ])

for postgres .query('SELECT * FROM test WHERE id = ANY($1)', [[1,2,4]]

below worked for me in case of mssql driver

enter image description here

for more info https://github.com/typeorm/typeorm/issues/556

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.