将多行html编码的字符串存储到sql中

时间:2018-05-08 08:17:13

标签: javascript sql

我想将html代码存储到我使用encodeURI()进行编码的sql数据库中,但它显示了多个错误,如下所示

error while inserting encoded html code

我使用dataType作为<CLOB>也尝试使用NVARCHAR(MAX),但显示相同的错误。

以字符串格式共享我编码的html代码以存储到sql。

%3Cp%3Ethis%20kind%20of%20text%20i'm%20storing%20into%20database%3C/p%3E%3Cpre%20class=%22code-pre%22%3Evar uri%20= %22my%20test.asp?name=st%C3%A5le&car=saab%22;%0Avar enc%20=%20encodeURI(uri);%0Avar dec%20=%20decodeURI(enc);%0Avar res%20=%20enc%20+ %22<br>%22 +%20dec;%0A%3C/pre%3E

INSERT INTO "Mytable" VALUES(
8/*ID <INTEGER>*/,
'Return matching objects from array of objects'/*QUESTION <NVARCHAR(200)>*/,
'%3Cp%3Ethis%20kind%20of%20text%20i'm%20storing%20into%20database%3C/p%3E%3C     pre%20class=%22code-pre%22%3Evar uri%20= %22my%20test.asp ? 
 name=st%C3%A5le&car=saab%22;%0Avar enc%20=%20encodeURI(uri);%0Avar 
 dec%20=%20decodeURI(enc);%0Avar res%20=%20enc%20+ %22<br>%22 
 +%20dec;%0A%3C/pre%3E'/*QUESTION_DESC <CLOB>*/,
'20170508'/*CREATED <TIMESTAMP>*/,
0/*USERID <INTEGER>*/,
1/*TAGID <INTEGER>*/
);

以上命令我用于将数据推送到数据库。 QUESTION_DESC字符串i&#39;已编码。原始字符串

<p>this kind of text i'm storing into database</p><pre class="code- 
pre">var&nbsp;uri =&nbsp;"my test.asp?name=ståle&amp;car=saab";
var&nbsp;enc = encodeURI(uri);
var&nbsp;dec = decodeURI(enc);
var&nbsp;res = enc +&nbsp;"&lt;br&gt;"&nbsp;+ dec;
</pre>

帮助将受到欢迎

1 个答案:

答案 0 :(得分:0)

当我尝试从中间件发布html代码时,这非常简单。 问题是当我试图将html代码发布到数据库时,由于一些随机双引号而显示错误。因此,当从中间件发送html代码时,我只是将双引号替换为忽略双引号。我确实喜欢

我的html代码存储在db

var htmlCodeToBeStored = 
"<p>this kind of text i'm storing into database</p><pre class="code- 
 pre">var&nbsp;uri =&nbsp;"my test.asp?name=ståle&amp;car=saab"; 
 var&nbsp;enc = encodeURI(uri);
 var&nbsp;dec = decodeURI(enc); 
 var&nbsp;res = enc +&nbsp;"&lt;br&gt;"&nbsp;+ dec;
 </pre>"

我用以下替换上面的字符串 htmlCodeToBeStored = htmlCodeToBeStored.replace(/"/g, "\"")

通过这种简单的改变,我能够将我的ans存储到数据库中。