1

我试图在这里问这个,但结果并不好。我考虑过编辑,但鉴于已经收到的答案,我认为最好重新开始。

我收到以下错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.' in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\sustainable_water_allocation\LTOO_test.php:348 Stack trace: #0 C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\sustainable_water_allocation\LTOO_test.php(348): PDOStatement->execute() #1 C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\sustainable_water_allocation\ga.php(119): totalSI(Object(gaParent), 1) #2 C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\sustainable_water_allocation\ga.php(266): GA->fitness(Object(gaParent), 'totalSI') #3 C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\sustainable_water_allocation\LT in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\sustainable_water_allocation\LTOO_test.php on line 348

这是连接设置:

$this->dbh = new PDO($dsn, $user, $password, array(
            PDO::ATTR_PERSISTENT => false,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::MYSQL_ATTR_USE_BUFFERED_QUERY =>true
            ));

这是 MySQL 通用日志文件:

    1 Connect
        1 Query select rva from demand_nodes
    1 Query select * from demand_nodes
    1 Query select * from source_nodes
    1 Query TRUNCATE TABLE  `results_demand`;
    1 Query TRUNCATE TABLE  `results_link_input`;
    1 Query TRUNCATE TABLE  `results_source_state`;
    1 Query TRUNCATE TABLE  `results_supply`
    1 Quit

下一个要运行的查询会生成错误。

实例化对象时会生成查询,如下所示:

$allSources = new sources();

为了更好地衡量,以下是所有列出的查询:

$sql = "select rva from demand_nodes";
$core = Core::getInstance();
$stmt = $core->dbh->prepare($sql);

if ($stmt->execute()) {
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        $rVAs = $rVAs + $row['rva'];
        $numOfDemands = $numOfDemands + 1;
    }
    $stmt->closeCursor(); 
}

$sql = "select * from demand_nodes";
$core = Core::getInstance();
$stmt = $core->dbh->prepare($sql);
$stmt->execute();
$row = $stmt->fetchAll();

foreach($row as $i=>$value){
    $this->id[] = $row[$i]['id'];
    $this->label[] = $row[$i]['label'];
    $this->initialDelta[] = $row[$i]['initial_delta'];
    $this->initialRate[] = $row[$i]['initial_rate'];
    $this->deltaMin[] = $row[$i]['delta_min'];
    $this->deltaMax[] = $row[$i]['delta_max'];
    $this->rateMin[] = $row[$i]['rate_min'];
    $this->rateMax[] = $row[$i]['rate_max'];
    if($row[$i]['si_weight'] != 0){
        $this->siWeight[] = $row[$i]['si_weight'];
    }else{
        $this->siWeight[] = 1/($numOfDemands + $rVAs); 
    }
    $this->rva[] = $row[$i]['rva'];
}

$sql = "select * from source_nodes";
$core = Core::getInstance();
$stmt = $core->dbh->prepare($sql);
$stmt->execute();
$row = $stmt->fetchAll();
foreach($row as $i=>$value){
    $this->id[] = $row[$i]['id'];
    $this->label[] = $row[$i]['label'];
    $this->state[] = $row[$i]['initial_state'];
}

难住了。我不确定还要添加什么,但如果我错过了什么,请告诉我。

4

2 回答 2

2

回答:

原来截断语句需要一个封闭的游标:

$sql = "TRUNCATE TABLE  `results_demand`;
    TRUNCATE TABLE  `results_link_input`;
    TRUNCATE TABLE  `results_source_state`;
    TRUNCATE TABLE  `results_supply`;";
$core = Core::getInstance();
$stmt = $core->dbh->prepare($sql);
$stmt->execute();
$stmt->closeCursor();

至于为什么:

我欣然承认我不明白closeCursor()为什么truncate. MySQL 表示 truncate 映射到deleteInnoDB;但是我查看的任何文档中都没有任何内容表明“为什么?”。

也许其他人可以谈论这个。

40 多个小时... =/ 但它已解决!

于 2013-08-10T19:00:04.843 回答
0

为了将来参考,我还需要在执行 DROP TABLE 后调用 closeCursor(),因此问题不仅限于 TRUNCATE。

于 2016-10-05T17:27:39.807 回答