1

请我在追踪我已经尝试了几个小时的这段代码时遇到了问题。它给了我错误 DBD::mysql::st fetchrow_hashref failed: fetch() without execute() 在第 15 行

        sub Split_Into_Words
        {
            #### Connection parameters ############################
            my $dsn =  "dbi:mysql:malware:localhost:3306";
            my $user = 'root';
            my $passwd = 'sxxxs';
            ########################################################
            my $domain ;
            my $countDir = 0 ;
            my $file = shift ;
            my $labelID  =  (split(/[.]/ , $file))[1] ; ### Split and get the middle value since format is temporay.

            #### Query String ############################################################################
             my $InsertIntoHostTable_QS  = "INSERT INTO TB_host(HostName  , UrlID , ExtID) Values (? , ? , ? ) ; ";
             my $InsertIntoDomainTable_QS = "INSERT IGNORE INTO  TB_Domain(Domain) values (?) ;" ;
             my $InsertIntoArgVal_QS = "INSERT INTO TB_Arg_Value(Arg, URL_ID)  VALUES (?  , ? ) ; " ; 
             my $InsertIntoDirectory_QS = "INSERT INTO TB_Directory(DIRNAME , DEPTH , URLID) VALUES (? , ? , ? )" ;
             my $InsertIntoExtension_QS = "INSERT IGNORE INTO TB_Extension (Extension) values ( ? ) ; ";
             my $InsertIntoExtensionNULL_QS =   "INSERT IGNORE INTO TB_Extension (ID , Extension) values (? , ? )  ; ";
             my $SelectString  = " Select URL , ID  from TB_URL where LabelID = '"  .  $labelID."';";
             my $InsertIntoFileName_QS  = "INSERT IGNORE INTO TB_FileName( filename)  VALUES (?) ; " ; 

             ###################################################################################################
             my $DBIConnect = DBI->connect($dsn , $user , $passwd) or die("Cannot connect to datadbase  $DBI::errstr\n");   


            print ("Splitting Into Words \n");


            ######Initialization of a default DB value #################
            my $sth =  $DBIConnect->prepare( $InsertIntoExtensionNULL_QS);
                    $sth->execute(1 , 'null') or die("Error Executing the Insertion" . $sth->errstr );
                    $sth->finish();
            #############################################################
            $sth =  $DBIConnect ->prepare($SelectString);
            sleep(10);
            open (FH , '<' , $file); # Open file to be read from disk

            my $i = 0;
            $sth->execute() or die("Error Executing the Insertion" . $sth->errstr );

   ->line 15        while(my $hash_ref = $sth->fetchrow_hashref )
            {
                    my $extensionID = "1";
                    my $intialURL =  $hash_ref->{URL} ;

                my $initialID = $hash_ref->{ID};
    }
    }
4

2 回答 2

2

我不确定这是否是问题,但插入后您可能不需要完成。来自DBI 文档

指示在再次执行或销毁之前,不会从此语句句柄中获取更多数据。您几乎可以肯定不需要调用此方法。

在获取所有行的循环之后添加对完成的调用是一个常见错误,不要这样做,它可以掩盖真正的问题,例如未捕获的获取错误。

如果这是问题所在,您可能需要为 select 调用创建第二个语句处理程序。

于 2011-08-17T21:40:52.543 回答
1

除了烦人的长 SQL 变量名之外,$SelectString 还应该包含一个“?”,以防 $labelID 包含可能破坏查询或导致注入的内容。

prepare() 不是绝对需要一个“?”,但是如果 execute 有参数,那么必须有一个匹配数量的“?” 在查询字符串中。

第一个 $sth->finish() 不是必需的,因为查询是一个插入并且不返回任何行。

第二个“死”应该是“执行查询时出错”,因为它执行 $SelectString

注意 SQL 约定是全部用大写,为了额外的安全,将字段名称括在反引号中。查询不以分号结尾。另请注意,“我的”变量是大括号之间的局部变量,{},因此我在 while 循环中的变量之后将不可用。

建议格式如下:

sub Split_Into_Words {
    #### Connection parameters ############################
    my $dsn =  "dbi:mysql:malware:localhost:3306";
    my $user = 'root';
    my $passwd = 'sxxxs';
    ########################################################
    my $domain ;
    my $countDir = 0 ;
    my $file = shift ;
    my $labelID  =  (split(/[.]/ , $file))[1] ; ### Split and get the middle value since format is temporary.

    #### Query String ############################################################################
    my $InsertIntoHostTable_QS    = "INSERT INTO `TB_host` (`HostName`,`UrlID`,`ExtID`) VALUES (?,?,?)";
    my $InsertIntoDomainTable_QS  = "INSERT IGNORE INTO `TB_Domain` (`Domain`) VALUES (?)";
    my $InsertIntoArgVal_QS       = "INSERT INTO `TB_Arg_Value` (`Arg`,`URL_ID`) VALUES (?,?)";.
    my $InsertIntoDirectory_QS    = "INSERT INTO `TB_Directory` (`DIRNAME`,`DEPTH`,`URLID`) VALUES (?,?,?)";
    my $InsertIntoExtension_QS    = "INSERT IGNORE INTO `TB_Extension` (`Extension`) VALUES (?)";
    my $InsertIntoExtensionNULL_QS= "INSERT IGNORE INTO `TB_Extension` (`ID`,`Extension`) VALUES (?,?)";
    my $SelectString              = "SELECT `URL`,`ID` FROM `TB_URL` WHERE `LabelID`=?";
    my $InsertIntoFileName_QS     = "INSERT IGNORE INTO `TB_FileName` (`filename`) VALUES (?)";

    ###################################################################################################
    my $DBIConnect = DBI->connect($dsn , $user , $passwd) or die("Cannot connect to datadbase  $DBI::errstr\n");

    print ("Splitting Into Words \n");

    ######Initialization of a default DB value #################
    my $sth =  $DBIConnect->prepare( $InsertIntoExtensionNULL_QS);
    $sth->execute(1 , 'null') or die("Error executing the Insertion: " . $sth->errstr );
    # $sth->finish(); # not needed because it's an insert

    #############################################################
    $sth =  $DBIConnect->prepare($SelectString);
    sleep(10);
    open (FH , "<$file"); # Open file to be read from disk

    my $i = 0;
    $sth->execute($labelID) or die("Error executing query: " . $sth->errstr );

    while(my $hash_ref = $sth->fetchrow_hashref ) {
        my $extensionID = "1";
        my $intialURL = $hash_ref->{URL};
        my $initialID = $hash_ref->{ID};

    }
于 2011-08-17T21:49:40.257 回答