如何插入包含撇号(单引号)的值?
插入带有撇号的值的正确 SQL 语法是什么?
Insert into Person
(First, Last)
Values
'Joe',
'O'Brien'
我不断收到错误,因为我认为 O 后面的撇号是该值的结束标记。
What is the correct SQL syntax to insert a value with an apostrophe in it?
Insert into Person
(First, Last)
Values
'Joe',
'O'Brien'
I keep getting an error as I think the apostrophe after the O is the ending tag for the value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
在 SQL 中转义撇号(即双引号字符):
这同样适用于 SELECT 查询:
撇号或单引号是 SQL 中的特殊字符,用于指定字符串数据的开头和结尾。这意味着要将其用作文字字符串数据的一部分,您需要
转义
特殊字符。对于单引号,这通常是通过加倍报价来完成的。 (两个单引号字符,而不是双引号而不是单引号。)注意:只有在通过原始 SQL 接口手动编辑数据时才应该担心这个问题,因为在开发之外编写查询并且测试应该很少发生。在代码中,有一些技术和框架(取决于您的堆栈)可以处理特殊字符的转义,SQL 注入等
Escape the apostrophe (i.e. double-up the single quote character) in your SQL:
The same applies to SELECT queries:
The apostrophe, or single quote, is a special character in SQL that specifies the beginning and end of string data. This means that to use it as part of your literal string data you need to
escape
the special character. With a single quote this is typically accomplished by doubling your quote. (Two single quote characters, not double-quote instead of a single quote.)Note: You should only ever worry about this issue when you manually edit data via a raw SQL interface since writing queries outside of development and testing should be a rare occurrence. In code there are techniques and frameworks (depending on your stack) that take care of escaping special characters, SQL injection, etc.
你只需要把单引号加倍......
You just have to double up on the single quotes...
您需要转义撇号。在 T-SQL 中,这是带有双撇号的,因此您的
插入 语句变为:
You need to escape the apostrophe. In T-SQL this is with a double apostrophe, so your
insert
statement becomes:因为单引号是用来表示字符串的开始和结束的;你需要逃避它。
简短的答案是使用两个单引号 -
''
- 以便 SQL 数据库将值存储为'
。看看使用 REPLACE 来清理传入的值:
您想要检查
''''
,如果它们存在于字符串中,则将其替换为'''''' 为了转义单独的单引号。
Because a single quote is used for indicating the start and end of a string; you need to escape it.
The short answer is to use two single quotes -
''
- in order for an SQL database to store the value as'
.Look at using REPLACE to sanitize incoming values:
You want to check for
''''
, and replace them if they exist in the string with''''''
in order to escape the lone single quote.以下 SQL 说明了此功能。
结果
The following SQL illustrates this functionality.
Results
可以通过调用 CHAR 函数插入撇号字符撇号的 ASCII 表查找 值 39。然后可以将字符串值与 连接运算符。
The apostrophe character can be inserted by calling the CHAR function with the apostrophe's ASCII table lookup value, 39. The string values can then be concatenated together with a concatenate operator.
eduffy 有一个好主意< /a>.他只是在他的代码示例中把它倒过来了。无论是在 JavaScript 中还是在 SQLite 中,您都可以用重音符号替换撇号。
他(我确信是无意的)将重音符号放置为字符串的分隔符,而不是替换 O'Brian 中的撇号。事实上,对于大多数情况来说,这是一个非常简单的解决方案。
eduffy had a good idea. He just got it backwards in his code example. Either in JavaScript or in SQLite you can replace the apostrophe with the accent symbol.
He (accidentally I am sure) placed the accent symbol as the delimiter for the string instead of replacing the apostrophe in O'Brian. This is in fact a terrifically simple solution for most cases.
使用双引号将值引起来。
use double quotation marks around the values.
转义撇号的另一种方法是编写字符串文字:
这是一种更好的方法,因为:
假设您有一个 Excel 列表,其中包含要上传到数据库的 1000 个姓名。您只需创建一个公式即可使用单元格内容生成 1000 个 INSERT 语句,而无需手动查找撇号。
它也适用于其他转义字符。例如加载正则表达式模式值,即 ^( *)(P|N)?( *)|( *)((<|>)\d\d?)?( *)|( ) (((?i)(在|不在)(?-i) ?(('[^']+')(, ?'[^']+'))))?( *) $ 放入表中。
Another way of escaping the apostrophe is to write a string literal:
This is a better approach, because:
Imagine you have an Excel list with 1000's of names you want to upload to your database. You may simply create a formula to generate 1000's of INSERT statements with your cell contents instead of looking manually for apostrophes.
It works for other escape characters too. For example loading a Regex pattern value, i.e. ^( *)(P|N)?( *)|( *)((<|>)\d\d?)?( *)|( )(((?i)(in|not in)(?-i) ?(('[^']+')(, ?'[^']+'))))?( *)$ into a table.
如果是静态文本,可以使用
两个单引号
代替一个,如下所示:如果您的文本不是静态的并且它是在存储过程参数中传递的,那么
If it is static text, you can use
two single quote
instead of one as below:If your text is not static and it is passed in Store procedure parameter then
使用反引号(在 ~ 键上)代替;
Use a backtick (on the ~ key) instead;
这就是我的数据作为 API 响应的样子,我想将其存储在 MYSQL 数据库中。它包含引号、HTML 代码等。
示例:-
SOLUTION
在插入时,后面跟着 JSON.stringify()
上面是 JSON 对象,下面是将数据插入 MySQL 的 SQL 查询。
它的工作....
This is how my data as API response looks like, which I want to store in the MYSQL database. It contains Quotes, HTML Code , etc.
Example:-
SOLUTION
While inserting , In followed JSON.stringify()
Above is the JSON object and below is the SQL Query that insert data into MySQL.
Its worked....
提供的解决方案无法正常工作,因为它在数据库中添加带有两个单引号的字符串,最简单的方法是在撇号(单引号)之前使用反斜杠。
the solution provided is not working fine, since it ads the string with two single quote in database, the simplest way is to use anti back slash before the apostrophe (single quote).