如何将html字符串保存到数据库中

时间:2013-04-03 11:48:25

标签: ios objective-c database nsstring

我有html string

<p><span style="color: #4f4f4f; font-family: Arial; font-size: 12px; font-style: normal; 
font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 
15.600000381469727px; orphans: 2; text-align: -webkit-left; text-indent: 0px; text-transform:
 none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -
webkit-text-stroke-width: 0px; background-color: #ffffff; display: inline !important; float: 
none;">Numerical estimation is key in many craft and technical jobs where the ability to 
quickly and accurately estimate material quantities is essential. The speed at which you can 
answer these questions is the critical measure, as most people could achieve a very high sco
re given unlimited time in which to answer. You can therefore expect 25-35 questions in 10 
minutes or so.</span></p>

我想将其存储在我的database中,但应用程序在执行查询时崩溃了。 我是否应该删除任何特定的symbol或任何东西,或者是因为它的长度?

3 个答案:

答案 0 :(得分:4)

您应该执行 SQL Escape 操作。

答案 1 :(得分:2)

只要你将它作为一个字符串插入就可以了。试试这个:

    $htmlString = '<p><span style="color: #4f4f4f; font-family: Arial; font-size: 12px; font-style: normal; 
    font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 
    15.600000381469727px; orphans: 2; text-align: -webkit-left; text-indent: 0px; text-transform:
     none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -
    webkit-text-stroke-width: 0px; background-color: #ffffff; display: inline !important; float: 
    none;">Numerical estimation is key in many craft and technical jobs where the ability to 
    quickly and accurately estimate material quantities is essential. The speed at which you can 
    answer these questions is the critical measure, as most people could achieve a very high sco
    re given unlimited time in which to answer. You can therefore expect 25-35 questions in 10 
    minutes or so.</span></p>';

我基本上将""替换为''因为你的字符串已经包含双引号而且会破坏。

正如Ismet Alkan所说,在插入数据时执行SQL Escapes始终是一种好习惯。

另外,请确保您的database列与字符串兼容,因此将类型设置为TEXT应该可以解决问题。

答案 2 :(得分:1)

link有帮助。从Michael Waterfall的link下载了示例代码,它运行得很好。