Sahsha
Sahsha

Reputation: 35

How to insert special characters in MySQL

I need to insert values in description column that have special characters. Can anyone guide.

INSERT INTO accident_report ( Occurrence_Date, accident_category, Occurrence_Time, Machine_Location, affected, Machine_category, Machine_Number, description)
VALUE 
('2021-05-03', 'Minor', '14:19', 'shed 2', 'machine', 'CNC', '23', 'test');

Upvotes: 0

Views: 7381

Answers (1)

Tony
Tony

Reputation: 10327

Almost all the characters you list can be directly inserted in to a text or char field without problems.

Depending on the quote character you use you only need to "escape" the same character, so when using single quotes ' you don't have to escape double quotes ". With the quote you can either escape it with a back slash \ or double up the character ''.

Back slash characters also need to be escaped or doubled up.

Here's an example of how to handle the different characters: https://www.db-fiddle.com/f/qiV1AsQdRYLZkUE4HAzWgu/1

create table accident_report (id integer, description text);

insert accident_report(id, description) values 
(1, '@#$%^&*()_+[{}]|:;"<>,.?/'),
(2, '\\'),
(3, '\''),
(4, '''');

select * from accident_report;

Interestingly the markdown code colouring doesn't quite get it right with the double quoted characters.

Upvotes: 2

Related Questions