A
A
andreikotovqweet2021-03-21 20:53:40
SQL
andreikotovqweet, 2021-03-21 20:53:40

Why doesn't backslash work in a database?

UPDATE `test` SET answer = '{"test":["qwerty","\"test\""]}' - with this query, if the data type of the column is TEXT, then the string is stored: {"test":[" qwerty",""test""]} - (without backslash) and such json becomes invalid. If the data type of the column is JSON, then it simply throws an error, as if the error was in JSON. As a result, saving the string - "test" (instead of with quotes) does not work. I will be glad to answer

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AUser0, 2021-03-22
@andreikotovqweet

Because with your code you are telling the SQL parser to "treat the character after the \ as plain text".
PS Moreover, according to all SQL rules, you must escape all characters with a backslash \ in general " ' \to avoid hacking through SQL injections:

UPDATE `test` SET answer = '{\"test\":[\"qwerty\",\"\\\"test\\\"\",\"-\'-\"]}'

S
Slava Rozhnev, 2021-03-22
@rozhnev

You need to escape the slash:

update `test` set `answer` = '{"test":["qwerty","\\"test\\""]}' ;

SQL fiddle

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question