2017-09-14 121 views
0

使用MySQL 5数据库SQL列做不到,我想与内嵌HTML数据库本身手动更新字段:MySQL的5 - 插入内嵌HTML内容转换成使用UPDATE

UPDATE employee SET bio = "<div style="text-align:center"> <H2>Employment History</H2> <table class='table' style="width:auto;margin-left:auto;margin-right:auto;"><tr><th>Year</th><th>Company</th></tr><tr><td>2013-14</td><td>IBM</td></tr><tr><td>2015-16</td><td>Microsoft</td></tr><tr><td>2016-17</td><td>Google</td></tr></table><H2>BIO</H2></div><p>John Smith started out as developer and became project manager.</p>" where ID = 100; 

(顺便说一句这不是一条线 - 其被包装成多行,当我复制/粘贴它MySQL的外壳内)

结果在以下错误:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'margin-left:auto' at line 1 
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'margin-right:auto' at line 1 

什么我可能做错了吗?有没有办法在UPDATE或ALTER SQL语句中使用HTML文件进行此更改?

+0

字符串值应该用引号括起来,或者更好 - 你应该使用准备好的语句。 – Dekel

回答

1

您正在围绕字符串使用与围绕字符串中的HTML属性使用相同的引号。您需要使用不同的引号或转义内部引号。

UPDATE employee SET bio = "<div style='text-align:center'> <H2>Employment History</H2> <table class='table' style='width:auto;margin-left:auto;margin-right:auto;'><tr><th>Year</th><th>Company</th></tr><tr><td>2013-14</td><td>IBM</td></tr><tr><td>2015-16</td><td>Microsoft</td></tr><tr><td>2016-17</td><td>Google</td></tr></table><H2>BIO</H2></div><p>John Smith started out as developer and became project manager.</p>" where ID = 100; 

如果你从一个客户端应用程序的语言这样做,用事先准备好的声明,而不是直接代入SQL字符串。

+0

我正在尝试使用MySQL shell进行此操作 - 无客户端语言。如何在MySQL中避免双引号? –

+0

得到它使用字符串文字(双引号“”为每个“和单引号使用反斜杠像这样\')。感谢您的帮助,@Barmar。 –

+0

你不需要转义单引号如果字符串用双引号分隔。 – Barmar