首页 > 代码库 > Oracle往列中插入html代码
Oracle往列中插入html代码
开发提了一个需求,需要往模板表中插入包含html代码的记录,表的ddl如下
create table WZ_SITEMSGTEMPLATE ( id NUMBER(19) not null, templatecontent NCLOB, createtime DATE, modeltype NUMBER(10) default (1) )
html代码如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> body, div, p, a, img, span, h2 { margin: 0; padding: 0; } body { line-height: 1; font-family: Arial, Helvetica, sans-serif; font-size: 16px; } a { text-decoration: none; } img { border: 0; } .clearfix { overflow: hidden; zoom: 1; } .container--yx { width: 750px; margin: 0 auto; } .container--yx .top_bar { height: 96px; padding: 0 24px; } .container--yx .top_bar .logo_box { float: left; width: 263px; height: 37px; padding-top: 30px; } .container--yx .top_bar .qrcode_box { width: 213px; float: right; padding-top: 18px; } .container--yx .top_bar .qrcode_box .qrcode_pic { float: left; width: 62px; height: 62px; margin-right: 10px; } .container--yx .top_bar .qrcode_box .qrcode_text { float: right; } .container--yx .top_bar .qrcode_box .qrcode_text .lg_text { font-size: 23px; font-weight: bold; } .container--yx .top_bar .qrcode_box .qrcode_text .lg_text .red_text { color: #bd2230; } .container--yx .banner { position: relative; width: 100%; height: 458px; background: url(‘http://static.xxxxxxxx.com/images/yx_banner.jpg‘) no-repeat; background-size: 100% 458px; } .container--yx .banner .price { position: absolute; top: 64px; left: 140px; color: #fff; } .container--yx .banner .price .mark { position: absolute; top: 26px; font-size: 82px; font-family: ‘Microsoft Yahei‘; vertical-align: top; } .container--yx .banner .price .num { position: absolute; left: 82px; font-size: 238px; font-family: ‘Arial, Helvetica, sans-serif‘; } .container--yx .banner .draw_coupon { position: absolute; top: 124px; right: 124px; } .container--yx .banner .draw_coupon .coupon_title { color: #fff; font-size: 36px; margin-bottom: 20px; } .container--yx .banner .draw_coupon .coupon_btn { padding: 5px 24px; color: #333; background: #fff; } .container--yx .banner .down_box { position: absolute; top: 310px; padding: 0 84px; color: #fff; line-height: 30px; } .container--yx .new_active { background: #f8f8f8; padding-bottom: 30px; } .container--yx .new_active h2 { font-size: 24px; line-height: 68px; text-align: center; font-weight: normal; } .container--yx .new_active .wrap { padding: 0 14px; } .container--yx .new_active .wrap img { width: 235px; height: 112px; } </style> </head> <body> <div class="container--yx"> <div class="top_bar clearfix"> <img class="logo_box" src="http://static.xxxxxxxx.com/images/yx_logo.png" > <div class="qrcode_box clearfix"> <img class="qrcode_pic" src="http://static.xxxxxxxx.com/images/yx_qrcode.png" > <div class="qrcode_text"> <p>首次下载App</p> <div class="lg_text"> <p class="red_text">领188元</p> <p>时尚礼包</p> </div> </div> </div> </div> <div class="banner"> <div class="up_box"> <div class="price"> <span class="mark">¥</span> <span class="num">#lqamt#</span> </div> <div class="draw_coupon"> <p class="coupon_title">网龄券</p> <a class="coupon_btn" href="http://member.xxxxxxxx.com/Acct/Promotion">点击领取 > </a> </div> </div> <div class="down_box"> <p> #lqcontent# </p> </div> </div> <div class="new_active"> <h2>最新活动</h2> <div class="wrap"> <a href="javascript:;"> <img src="http://static.xxxxxxxx.com/images/yx_pic01.jpg" > </a> <a href="javascript:;"> <img src="http://static.xxxxxxxx.com/images/yx_pic02.jpg" > </a> <a href="javascript:;"> <img src="http://static.xxxxxxxx.com/images/yx_pic03.jpg" > </a> </div> </div> <div class="footer_bar"> <img src="http://static.xxxxxxxx.com/images/yx_bd.jpg" > </div> </div> </body> </html>
直接使用语句插入的时候,出现了“ORA-01756:引号内的字符串没有正确结束”的错误。
我把代码截断一下,不用看起来那么费劲,重现一下错误
select ‘[<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> body, div, p, a, img, span, h2 { margin: 0 ; padding: 0;]‘ from dual;
上面的代码,无论是在PL/SQL Developer里执行,还是在linux终端的sqlplus里执行,都是一样的错误。
有2个情况需要在这里说明一下
一、在linux终端的sqlplus里执行的时候,修改一下sqlterminator为非‘;‘的值,就能正常执行。
二、我的PL/SQL Developer(版本:9.0.6)执行报错,但是旁边同事的PL/SQL Developer(版本:11.0.x)就执行成功,不确定是不是版本的问题,也可能是首选项或者其他配置的差异,至今没找到差异在哪里。
google了一下,也没有搜索到答案,最终郑松华大师给了一个解决方法
select ‘[<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> body, div, p, a, img, span, h2 { margin: 0 ‘||‘;‘||‘ padding: 0;]‘ from dual;
原因是在oracle里,“;+换行”就是代表执行,所以就会提示上面的错误。但是让我疑惑的是,在字符串内的,为什么会执行?不到“‘”结束,也只是一段字符串而已。
此方法一;
补充:
因为html代码太长,使用这种方法插入到列的时候,会提示“ORA-01489:字符串连接的结果过长”。其中原因是把这些代码当做字符串来拼接,最大的长度不能超过varchar2的长度,解决方法是:把代码分开转换成nclob类型,再进行拼接,完整例子如下:
select seq_wz_sitemsgtemplate.nextval, to_nclob(‘<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> body, div, p, a, img, span, h2 { margin: 0‘||‘;‘||‘ padding: 0‘||‘;‘||‘ } body { line-height: 1‘||‘;‘||‘ font-family: Arial, Helvetica, sans-serif‘||‘;‘||‘ font-size: 16px‘||‘;‘||‘ } a { text-decoration: none‘||‘;‘||‘ } img { border: 0‘||‘;‘||‘ } .clearfix { overflow: hidden‘||‘;‘||‘ zoom: 1‘||‘;‘||‘ } .container--yx { width: 750px‘||‘;‘||‘ margin: 0 auto‘||‘;‘||‘ } .container--yx .top_bar { height: 96px‘||‘;‘||‘ padding: 0 24px‘||‘;‘||‘ } .container--yx .top_bar .logo_box { float: left‘||‘;‘||‘ width: 263px‘||‘;‘||‘ height: 37px‘||‘;‘||‘ padding-top: 30px‘||‘;‘||‘ } .container--yx .top_bar .qrcode_box { width: 213px‘||‘;‘||‘ float: right‘||‘;‘||‘ padding-top: 18px‘||‘;‘||‘ } .container--yx .top_bar .qrcode_box .qrcode_pic { float: left‘||‘;‘||‘ width: 62px‘||‘;‘||‘ height: 62px‘||‘;‘||‘ margin-right: 10px‘||‘;‘||‘ } .container--yx .top_bar .qrcode_box .qrcode_text { float: right‘||‘;‘||‘ } .container--yx .top_bar .qrcode_box .qrcode_text .lg_text { font-size: 23px‘||‘;‘||‘ font-weight: bold‘||‘;‘||‘ } .container--yx .top_bar .qrcode_box .qrcode_text .lg_text .red_text { color: #bd2230‘||‘;‘||‘ } .container--yx .banner { position: relative‘||‘;‘||‘ width: 100%‘||‘;‘||‘ height: 458px‘||‘;‘||‘ background: url(‘‘http://static.xxxxxxxx.com/images/yx_banner.jpg‘‘) no-repeat‘||‘;‘||‘ background-size: 100% 458px‘||‘;‘||‘ } .container--yx .banner .price { position: absolute‘||‘;‘||‘ top: 64px‘||‘;‘||‘ left: 140px‘||‘;‘||‘ color: #fff‘||‘;‘||‘ } .container--yx .banner .price .mark { position: absolute‘||‘;‘||‘ top: 26px‘||‘;‘||‘ font-size: 82px‘||‘;‘||‘ font-family: ‘‘Microsoft Yahei‘ || ‘‘||‘;‘||‘ vertical-align: top‘||‘;‘||‘ } .container--yx .banner .price .num { position: absolute‘||‘;‘||‘ left: 82px‘||‘;‘||‘ font-size: 238px‘||‘;‘||‘ font-family: ‘‘Arial, Helvetica, sans-serif‘ || ‘‘||‘;‘||‘ } .container--yx .banner .draw_coupon { position: absolute‘||‘;‘||‘ top: 124px‘||‘;‘||‘ right: 124px‘||‘;‘||‘ } .container--yx .banner .draw_coupon .coupon_title { color: #fff‘||‘;‘||‘ font-size: 36px‘||‘;‘||‘ margin-bottom: 20px‘||‘;‘||‘ } .container--yx .banner .draw_coupon .coupon_btn { padding: 5px 24px‘||‘;‘||‘ color: #333‘||‘;‘||‘ background: #fff‘||‘;‘||‘ } .container--yx .banner .down_box { position: absolute‘||‘;‘||‘ top: 310px‘||‘;‘||‘ padding: 0 84px‘||‘;‘||‘ color: #fff‘||‘;‘||‘ line-height: 30px‘||‘;‘||‘ } .container--yx .new_active { background: #f8f8f8‘||‘;‘||‘ padding-bottom: 30px‘||‘;‘||‘ } .container--yx .new_active h2 { font-size: 24px‘||‘;‘||‘ line-height: 68px‘||‘;‘||‘ text-align: center‘||‘;‘||‘ font-weight: normal‘||‘;‘||‘ } .container--yx .new_active .wrap { padding: 0 14px‘||‘;‘||‘ } .container--yx .new_active .wrap img { width: 235px‘||‘;‘||‘ height: 112px‘||‘;‘||‘ } </style> </head>‘) || to_nclob(‘ <body> <div class="container--yx"> <div class="top_bar clearfix"> <img class="logo_box" src="http://static.xxxxxxxx.com/images/yx_logo.png" > <div class="qrcode_box clearfix"> <img class="qrcode_pic" src="http://static.xxxxxxxx.com/images/yx_qrcode.png" > <div class="qrcode_text"> <p>首次下载App</p> <div class="lg_text"> <p class="red_text">领188元</p> <p>时尚礼包</p> </div> </div> </div> </div> <div class="banner"> <div class="up_box"> <div class="price"> <span class="mark">¥</span> <span class="num">#lqamt#</span> </div> <div class="draw_coupon"> <p class="coupon_title">网龄券</p> <a class="coupon_btn" href="http://member.xxxxxxxx.com/Acct/Promotion">点击领取 > </a> </div> </div> <div class="down_box"> <p> #lqcontent# </p> </div> </div> <div class="new_active"> <h2>最新活动</h2> <div class="wrap"> <a href="javascript:‘||‘;‘||‘ "> <img src="http://static.xxxxxxxx.com/images/yx_pic01.jpg" > </a> <a href="javascript:‘||‘;‘||‘ "> <img src="http://static.xxxxxxxx.com/images/yx_pic02.jpg" > </a> <a href="javascript:‘||‘;‘||‘ "> <img src="http://static.xxxxxxxx.com/images/yx_pic03.jpg" > </a> </div> </div> <div class="footer_bar"> <img src="http://static.xxxxxxxx.com/images/yx_bd.jpg" > </div> </div> </body> </html>‘) , sysdate, 2 from dual;
方法二:先插入其他的列,在使用select …… for update来粘贴html代码列。
select * from wz_sitemsgtemplate where id = 4388 for update ;
以上,如有错谬,请不吝指正。
Oracle往列中插入html代码