首页 > 解决方案 > 如何在 MySQL 中插入特殊字符

问题描述

我需要在具有特殊字符的描述列中插入值。任何人都可以指导。

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');

标签: mysqlspecial-characters

解决方案


您列出的几乎所有字符都可以直接插入到textorchar字段中而不会出现问题。

根据您使用的引号字符,您只需要“转义”相同的字符,因此在使用单引号时'您不必转义双引号"。使用引号,您可以使用反斜杠对其进行转义,也可以\将字符加倍''

反斜杠字符也需要转义或加倍。

这是如何处理不同字符的示例: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;

有趣的是,markdown 代码着色与双引号字符并不完全正确。


推荐阅读