如何插入包含撇号(单引号)的值?

发布于 2024-08-15 04:31:20 字数 157 浏览 2 评论 0原文

插入带有撇号的值的正确 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

捂风挽笑 2024-08-22 04:31:20

在 SQL 中转义撇号(即双引号字符):

INSERT INTO Person
    (First, Last)
VALUES
    ('Joe', 'O''Brien')
              /\
          right here  

这同样适用于 SELECT 查询:

SELECT First, Last FROM Person WHERE Last = 'O''Brien'

撇号或单引号是 SQL 中的特殊字符,用于指定字符串数据的开头和结尾。这意味着要将其用作文字字符串数据的一部分,您需要转义特殊字符。对于单引号,这通常是通过加倍报价来完成的。 (两个单引号字符,而不是双引号而不是单引号。)

注意:只有在通过原始 SQL 接口手动编辑数据时才应该担心这个问题,因为在开发之外编写查询并且测试应该很少发生。在代码中,有一些技术和框架(取决于您的堆栈)可以处理特殊字符的转义,SQL 注入等

Escape the apostrophe (i.e. double-up the single quote character) in your SQL:

INSERT INTO Person
    (First, Last)
VALUES
    ('Joe', 'O''Brien')
              /\
          right here  

The same applies to SELECT queries:

SELECT First, Last FROM Person WHERE Last = 'O''Brien'

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.

孤独患者 2024-08-22 04:31:20

你只需要把单引号加倍......

insert into Person (First, Last)
values ('Joe', 'O''Brien')

You just have to double up on the single quotes...

insert into Person (First, Last)
values ('Joe', 'O''Brien')
不交电费瞎发啥光 2024-08-22 04:31:20

您需要转义撇号。在 T-SQL 中,这是带有双撇号的,因此您的插入 语句变为:

Insert into Person
(First, Last)
Values
'Joe', 'O''Brien'

You need to escape the apostrophe. In T-SQL this is with a double apostrophe, so your insert statement becomes:

Insert into Person
(First, Last)
Values
'Joe', 'O''Brien'
贱人配狗天长地久 2024-08-22 04:31:20

因为单引号是用来表示字符串的开始和结束的;你需要逃避它。

简短的答案是使用两个单引号 - '' - 以便 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.

A君 2024-08-22 04:31:20

单引号通过加倍转义,

以下 SQL 说明了此功能。

declare @person TABLE (
    [First] nvarchar(200),
    [Last] nvarchar(200)
)

insert into @person 
    (First, Last)
values
    ('Joe', 'O''Brien')

select * from @person

结果

First   | Last
===================
Joe     | O'Brien

Single quotes are escaped by doubling them up,

The following SQL illustrates this functionality.

declare @person TABLE (
    [First] nvarchar(200),
    [Last] nvarchar(200)
)

insert into @person 
    (First, Last)
values
    ('Joe', 'O''Brien')

select * from @person

Results

First   | Last
===================
Joe     | O'Brien
献世佛 2024-08-22 04:31:20

可以通过调用 CHAR 函数插入撇号字符撇号的 ASCII 表查找 值 39。然后可以将字符串值与 连接运算符

Insert into Person
  (First, Last)
Values
  'Joe',
  concat('O',char(39),'Brien')

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.

Insert into Person
  (First, Last)
Values
  'Joe',
  concat('O',char(39),'Brien')
真心难拥有 2024-08-22 04:31:20

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.

旧城空念 2024-08-22 04:31:20

使用双引号将值引起来。

insert into Person (First, Last) Values("Joe","O'Brien")

use double quotation marks around the values.

insert into Person (First, Last) Values("Joe","O'Brien")
戴着白色围巾的女孩 2024-08-22 04:31:20

转义撇号的另一种方法是编写字符串文字:

insert into Person (First, Last) values (q'[Joe]', q'[O'Brien]')

这是一种更好的方法,因为:

  1. 假设您有一个 Excel 列表,其中包含要上传到数据库的 1000 个姓名。您只需创建一个公式即可使用单元格内容生成 1000 个 INSERT 语句,而无需手动查找撇号。

  2. 它也适用于其他转义字符。例如加载正则表达式模式值,即 ^( *)(P|N)?( *)|( *)((<|>)\d\d?)?( *)|( ) (((?i)(在|不在)(?-i) ?(('[^']+')(, ?'[^']+'))))?( *) $ 放入表中。

Another way of escaping the apostrophe is to write a string literal:

insert into Person (First, Last) values (q'[Joe]', q'[O'Brien]')

This is a better approach, because:

  1. 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.

  2. 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.

腹黑女流氓 2024-08-22 04:31:20

如果是静态文本,可以使用两个单引号代替一个,如下所示:

DEC @text = 'Khabir''s Account'

看到Khabir后面有两个单引号('')

如果您的文本不是静态的并且它是在存储过程参数中传递的,那么

REPLACE(@text, '''', '')

If it is static text, you can use two single quote instead of one as below:

DEC @text = 'Khabir''s Account'

See after Khabir there are two single quote ('')

If your text is not static and it is passed in Store procedure parameter then

REPLACE(@text, '''', '')
明媚如初 2024-08-22 04:31:20

使用反引号(在 ~ 键上)代替;

`O'Brien`

Use a backtick (on the ~ key) instead;

`O'Brien`
情未る 2024-08-22 04:31:20

这就是我的数据作为 API 响应的样子,我想将其存储在 MYSQL 数据库中。它包含引号、HTML 代码等。

示例:-

{

rewardName: "Cabela's eGiftCard $25.00",

shortDescription: '<p>adidas gift cards can be redeemed in over 150 adidas Sport Performance, adidas Originals, or adidas Outlet stores in the US, as well as online at <a href="http://adidas.com/">adidas.com</a>.</p>

terms: '<p>adidas Gift Cards may be redeemed for merchandise on <a href="http://adidas.com/">adidas.com</a> and in adidas Sport Performance, adidas Originals, and adidas Outlet stores in the United States.'

}

SOLUTION

CREATE TABLE `brand` (
`reward_name` varchar(2048),
`short_description` varchar(2048),
`terms` varchar(2048),  
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;

在插入时,后面跟着 JSON.stringify()

    let brandDetails= {
    rewardName: JSON.stringify(obj.rewardName),  
    shortDescription: JSON.stringify(obj.shortDescription),
    term: JSON.stringify(obj.term),
     }

上面是 JSON 对象,下面是将数据插入 MySQL 的 SQL 查询。

let query = `INSERT INTO brand (reward_name, short_description, terms) 
VALUES (${brandDetails.rewardName}, 
(${brandDetails.shortDescription}, ${brandDetails.terms})`;

它的工作....

在此处输入图像描述

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:-

{

rewardName: "Cabela's eGiftCard $25.00",

shortDescription: '<p>adidas gift cards can be redeemed in over 150 adidas Sport Performance, adidas Originals, or adidas Outlet stores in the US, as well as online at <a href="http://adidas.com/">adidas.com</a>.</p>

terms: '<p>adidas Gift Cards may be redeemed for merchandise on <a href="http://adidas.com/">adidas.com</a> and in adidas Sport Performance, adidas Originals, and adidas Outlet stores in the United States.'

}

SOLUTION

CREATE TABLE `brand` (
`reward_name` varchar(2048),
`short_description` varchar(2048),
`terms` varchar(2048),  
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;

While inserting , In followed JSON.stringify()

    let brandDetails= {
    rewardName: JSON.stringify(obj.rewardName),  
    shortDescription: JSON.stringify(obj.shortDescription),
    term: JSON.stringify(obj.term),
     }

Above is the JSON object and below is the SQL Query that insert data into MySQL.

let query = `INSERT INTO brand (reward_name, short_description, terms) 
VALUES (${brandDetails.rewardName}, 
(${brandDetails.shortDescription}, ${brandDetails.terms})`;

Its worked....

enter image description here

纸短情长 2024-08-22 04:31:20

提供的解决方案无法正常工作,因为它在数据库中添加带有两个单引号的字符串,最简单的方法是在撇号(单引号)之前使用反斜杠。

Insert into Person  (First, Last) Values  'Joe',  'O\'Brien'

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).

Insert into Person  (First, Last) Values  'Joe',  'O\'Brien'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文