What is idea behind escape characters?

Dear Experts

I am starting with mySQL and I cannot understand all the concept of escape characters in strings.

What was idea behind this? I have feeling that it makes my life just more difficult if I want to assure that string which I have in variable is indeed stored in mySQL in exact form.

Many thanks for your advices

Vladimir
Vladimir BuzalkaCRAAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Just consider this case:

We have no escaping. The string delimiter is fixed to '. Write an INSERT statement which inserts "D'oh?!" into a table.



Same applies to object naming. Without delimiters here, we would be limited to a subset of ASCII (7bit) only.
1
Paul NeralichBusiness OwnerCommented:
Hi Vladimir,
Adding a backslash character (\) before certain other characters forms a special command. For example:

\n "escapes" the literal letter "n" and instead is interpreted as a new line:

mysql> SELECT 'Paul nNeralich';
Paul nNeralich

mysql> SELECT 'Paul \nNeralich';
Paul
Neralich
0
Paul NeralichBusiness OwnerCommented:
Can you give us an example of something you are trying to write to the database for which you feel you need to make use of escaping?
1
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
You asked, *"What is idea behind escape characters?"*

Specifically, MariaDB/MySQL use some special characters in normal syntax.

Escaping is required, so these special characters can be... disabled as syntax + enabled as normal characters...

Without escaping, you could never write some characters as data.
0
Paul NeralichBusiness OwnerCommented:
ste5an's example of handing the string   D'oh?!   is an excellent example. To illustrate David's point, we need the single quote in position 2 of this string to be treated as normal text, otherwise SQL will interpret it as the end of the string. Therefore the backslash is used to escape the syntax and treat that particular single quote as text. Consider these examples:

No good:
INSERT INTO HomerQuotes
(pkey, text)
VALUES
(1,'D'oh?!');

Above, SQL would interpret the string to bea single character: D
and not know what to do with the: oh?!'
Escaping that single quote solves the problem:

INSERT INTO HomerQuotes
(pkey, text)
VALUES
(1,'D\'oh?!');


(I first learned about the concept of escaping characters when I learned HP's flavor of Unix, HP-UX)
0
Chris StanyonWebDevCommented:
All the comments above explain the reasons behind escaping values.

However, if you're writing a script (such as PHP or C#) to insert into your database, then you can avoid the need to escape your variables altogether. You do this by writing prepared statements. Because your variables are never wrapped in quotes, there's no need to escape them.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vladimir BuzalkaCRAAuthor Commented:
Dear Experts

many thanks for your explanations, I think I can understand need for character escaping now.

Thanks a lot again

V
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.