【问题标题】:General error 2014 with PDO buffer attribute set to truePDO 缓冲区属性设置为 true 的一般错误 2014
【发布时间】:2013-08-12 00:26:18
【问题描述】:

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

我收到以下错误:

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'];
}

难住了。我不知道还有什么要补充的,但如果我遗漏了什么,请告诉我。

【问题讨论】:

  • 这次你做得更好但又一次:我们怎么知道an object is being instantiated, like this:$allSources = new sources();时发生了什么?我们怎么能?此外,错误消息包含一个堆栈跟踪,可以帮助跟踪错误源。它被省略再次。请注意,要获得可靠的答案,您必须提出可靠的问题。
  • 顺便说一句,你填充几十个独立数组(如$this->id[])的想法相当很奇怪。此外,对demand_nodes两个 查询看起来也相当 很奇怪。此外,您需要每个表中的所有条记录的这种设计看起来又相当很奇怪。
  • Strange 和我想的一样奇怪。这是造成问题的原因吗?正如我在上一篇文章中提到的,我几乎没有接受过正式的编程培训。这不是一个网站,它是我正在开发的水分配和优化模型。这样做对我来说很有意义,但如果它影响运行时性能或导致错误,我不介意更改它。如有任何需要,请随时提出;我包括了我认为重要的内容。我只关心解决方案。
  • 这个错误意味着一些上层查询未完成。使用堆栈跟踪来确定调用者代码,在里面哦,这个循环被调用了。
  • 我不知道该怎么做,但我会看看。与此同时 - 一般的 mysql 日志显示所有查询。所有查询设置都发布在问题中。任何会导致此错误的查询有什么问题吗?

标签: mysql pdo truncate buffered


【解决方案1】:

答案:

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

$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();

至于原因:

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-08
      • 2018-09-09
      • 2022-11-02
      • 2015-08-16
      • 2013-06-20
      相关资源
      最近更新 更多