免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2512 | 回复: 5
打印 上一主题 下一主题

请教perl与mysql连接问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-04-28 17:19 |只看该作者 |倒序浏览
我想将一个文件中的内容添加到数据库中,其中文件格式如下:


  1. ......

  2. body DEAR_SOMETHING                /\bDear (?:IT\W|Internet|candidate|sirs?|madam|investor|travell?er|car shopper|web)\b/i
  3. describe DEAR_SOMETHING                Contains 'Dear (something)'
  4. score DEAR_SOMETHING 1.483

  5. header DATE_IN_PAST_24_48             eval:check_for_shifted_date('-48', '-24')
  6. describe DATE_IN_PAST_24_48        Date: is 24 to 48 hours before Received: date
  7. score DATE_IN_PAST_24_48 1.955

  8. ......
复制代码


代码如下:

  1.     open FH,$rulefile or die "Cant open the rule file";

  2.     my %formscore,%formregex,%formdescribe,%formtype;
  3.    
  4.        while($line=<FH>){   
  5.         $line=~s/^#.*$//;
  6.         $line=~s/^\s+//;
  7.         $line=~s/\s+$//;
  8.         next unless($line);
  9.         ($type,$name,$detail)=split (/[\s\t]+/,$line,3);
  10.         if($type eq "score"){
  11.             $formscore{$name}=$detail;
  12.         }elsif($type eq "describe"){
  13.             $formdescrgibe{$name}=$detail;
  14.         }else{
  15.             $formtype{$name}=$type;
  16.             $formregex{$name}=$detail;
  17.         }
  18.     }
  19.     close FH;

  20.     @number=keys(%formscore);  


  21.     foreach my $temp (@number){
  22.         $sth1 = $dbh->prepare("select count(name) from rule where name = '$temp'") or die "cant prepare!!!!\n";
  23.         $sth1->execute or die "cant execute!!!!\n";

  24.         $check = $sth1->fetchrow_array();

  25.         if($check== 0){  ###insert if not exist

  26.             $sth2 = $dbh->prepare("insert into rule values($temp,$formtype{$temp},$formdescribe{$temp},'$formregex{$temp}',$formscore{$temp})") or die "cant prepare!!!!\n";
  27.             $sth2->execute or die "cant execute!!!!\n";

  28.         }
  29.     }
复制代码


错误信息如下:


  1. [Sun Apr 22 03:51:43 2007] [error] [client 202.115.30.166] DBD::mysql::st execute failed: 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 '/\\bDear (NULL:IT\\W|Internet|candidate|sirsNULL|madam|investor|travellNULLer|car ' at line 1 at /usr/local/apache/cgi-bin/aone/test.cgi line 94., referer: http://202.115.30.241/cgi-bin/aone/rule.cgi
  2. [Sun Apr 22 03:51:43 2007] [error] [client 202.115.30.166] cant execute!!!!, referer: http://202.115.30.241/cgi-bin/aone/rule.cgi
  3. [Sun Apr 22 03:51:43 2007] [error] [client 202.115.30.166] Premature end of script headers: test.cgi, referer: http://202.115.30.241/cgi-bin/aone/rule.cgi
复制代码


请朋友们多多指教。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
2 [报告]
发表于 2007-04-28 17:25 |只看该作者
values( ) 里面的东西要带单引号的。

论坛徽章:
0
3 [报告]
发表于 2007-04-28 17:56 |只看该作者

回复 2楼 flw 的帖子

感谢flw。

可是还是有点问题

  1. [Sun Apr 22 04:28:56 2007] [error] [client 202.115.30.166] DBD::mysql::st execute failed: 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 'max_shouting','3','4')',0)' at line 1 at /usr/local/apache/cgi-bin/aone/test.cgi line 95., referer: http://202.115.30.241/cgi-bin/aone/rule.cgi
  2. [Sun Apr 22 04:28:56 2007] [error] [client 202.115.30.166] cant execute!!!!, referer: http://202.115.30.241/cgi-bin/aone/rule.cgi
  3. [Sun Apr 22 04:28:56 2007] [error] [client 202.115.30.166] Premature end of script headers: test.cgi, referer: http://202.115.30.241/cgi-bin/aone/rule.cgi
复制代码


这个是因为文件中存在eval:

  1. body HTML_SHOUTING3                eval:html_range('max_shouting','2','3')
  2. describe HTML_SHOUTING3                HTML has very strong "shouting" markup
  3. score HTML_SHOUTING3 0.250
复制代码


总是表达式部分出问题,不晓得怎么回事,感觉应该是存在转义字符,我只是想简单的把各字段的值存到数据库中而已。

论坛徽章:
0
4 [报告]
发表于 2007-04-28 17:59 |只看该作者
你先把sql语句打印出来,然后把这个sql语句放到mysql里执行一次看出什么错?然后再做相应修改。

论坛徽章:
0
5 [报告]
发表于 2007-04-28 18:30 |只看该作者
感谢福瑞哈哥。



  1. $type="body";
  2. $name="dddd";
  3. $describ="testdddd";
  4. $regex="eval:html_range('obfuscation_ratio','.9','1.0')";
  5. $score=1.251;

  6. $sentence="insert into rule values('$type','$name','$describ','$regex',$score)";
  7. print $sentence;

  8. my $sth = $dbh->prepare(qq{$sentence}) or die "cant prepare!!!!\n";
  9. $sth->execute or die "cant execute!!!!\n";
复制代码


输出如下:

  1. DBD::mysql::st execute failed: 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 'obfuscation_ratio','.9','1.0')',1.251)' at line 1 at ./testdb.pl line 55.
  2. cant execute!!!!
  3. insert into rule values('body','dddd','testdddd','eval:html_range('obfuscation_ratio','.9','1.0')',1.251)
复制代码



对于转义字符再应该怎么办呢?

论坛徽章:
0
6 [报告]
发表于 2007-04-28 19:18 |只看该作者
呵呵
知道了
用DBI->quote()来进行转义。



结帖
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP