0

I'm writing a function that logs interactions with a Facebook messenger bot. Anything the user says is logged in a PostgreSQL database.

My insert statement looks like this:

INSERT INTO interactions (fbid,date,time,event) VALUES ('senderid','2018-10- 
   01','11:15:48','text')

Treat "senderid" as a 20-digit number and "text" as whatever the user says.

Now, the above statement works IF the text from the user contains no apostrophe characters. However, sometimes the text DOES contain apostrophes. In these cases, the insert doesn't work.

For example, if the user says "Let's chat" then my SQL looks like this:

INSERT INTO interactions (fbid,date,time,event) VALUES 
     ('senderid','2018-10-01','11:15:19','Let's Chat!')

and I get the following error:

Query result: error: syntax error at or near "s"

I know this is because I would need to escape the apostrophe. I've tried working around this by using double quotes in my insert statement, like this:

INSERT INTO interactions (fbid,date,time,event) VALUES ("senderid","2018-09- 
   28","10:50:07","Let's chat")

But when I do this I get the following error:

ERROR:  column "senderid" does not exist

So I have two questions:

  1. CAN I use double quotes in a SQL insert?
  2. If I CAN'T use double quotes in a SQL insert, how would I escape an apostrophe character, bearing in mind there may not always BE an apostrophe?

2 Answers2

0

Try below: you need to use single quote twice in case of apostrophi like let's will be let''s

INSERT INTO interactions (fbid,date,time,event) VALUES ('senderid','2018-09- 
   28','10:50:07','Let''s chat')
Fahmi
  • 37,315
  • 5
  • 22
  • 31
0

1.CAN I use double quotes in a SQL insert?

no you can't because double quote means column name in postgrey that why when sql engine found that column when you use double quote

you can use

'Let''s Chat!'
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63