2016-01-25 10 views
2

我有以下文字:如何用'进入表格中输入文字?

Price is ..... 

return policy is.... 

Mega's accept.... 

我想这整个文本插入式TEXT

的专栏中,我写的是什么:

Insert into A (id,msg) values (5, ' Price is ..... 

    return policy is.... 

    Mega's store online ....') 

Mega's是的名称商店。在这个名称的'导致查询认为它是文本的结尾...

所以我得到的错误信息:

ERROR: column "Price is ....." does not exist

我试图与选择插入,但也不至于工作。我收到相同的错误消息。

如何执行此插入操作?

+0

你是如何创建/运行INSERT语句?这是在查询窗口还是来自您写的应用程序? –

+0

不管你是否认为SQL注入是一个问题,没有理由不避免它,尤其是考虑到它对代码的简单编码。 –

+0

考虑到使用[预准备语句](https://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html)是多么容易,真的没有理由不使用它们。我只是为了不必担心逃跑角色而使用它。 –

回答

2

如果您不想(或不能)更改实际文字,请使用dollar quoting。只需将例如第一个和最后一个'替换为例如$$

Insert into A (id,msg) values (5, $$ Price is ..... 
    return policy is.... 
    Mega's store online ....$$) 

您需要使用在实际文本中不会出现的分隔符。如果您的文本可以包含$$你可以使用不同的东西,e.g:$not_there$

Insert into A (id,msg) values (5, $not_there$ Price is ..... 
    return policy is.... 
    Mega's store online ....$not_there$) 
+0

工作。thx – java

2

您可以将字符串中的单引号加一倍:'Mega''s store online'

或者,您可以通过函数quote_literal()传递任何字符串,然后将处理适当的引用和其他问题,例如SQL-injection。当字符串来自允许字符串中的单引号的某个应用程序时,这非常有用。

INSERT命令应该是:

Insert into A (id,msg) values (5, ' Price is .....' 
'return policy is....' 
'Mega''s store online ....'); 

可以多行打破了一长串,但每行必须有一个开闭单引号。

+0

有没有什么我可以做,而不真正阅读文本?我读过可以使用$ instad的'但不能使它工作。 – java

0

我会用CHAR(39)。

例子:

Mega' + CHAR(39) + 's store online....' 

参考:http://www.techonthenet.com/ascii/chart.php

+0

我的歉意 - 没有注意到你的postgres标签。美元符号应该是你的答案 –