经过多次操作后,来自PHP的SQL连接错误 [英] SQL connection error from PHP after many operations

查看:91
本文介绍了经过多次操作后,来自PHP的SQL连接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在循环创建MBTiles映射,并每次都将信息添加到我的数据库中. 这是我在循环期间配置连接和执行操作的方式:

I'm currently looping to create a MBTiles map, and add information to my database each time. Here's how I configured my connection and execute actions during the loop:

if ($pdo_mbtiles == null) {
    echo "Opening new database connection".PHP_EOL;
    $pdo_mbtiles = new PDO('sqlite:'.$filename,
            '',
            '',
            array(
                PDO::ATTR_PERSISTENT => true
                )
            );
    $pdo_mbtiles->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    $pdo_mbtiles->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}


$q = $pdo_mbtiles->prepare("INSERT INTO tiles (zoom_level, tile_column, tile_row,tile_data) VALUES (:zoom_level, :tile_column, :tile_rowTMS, :tile_data)");
$q->bindParam(':zoom_level', $zoom_level);
$q->bindParam(':tile_column', $tile_column);
$q->bindParam(':tile_rowTMS', $tile_rowTMS);
$q->bindParam(':tile_data', $tile_data, PDO::PARAM_LOB);
$q->execute();

循环1018次后(无论尝试多少次,此数字都不会改变),我收到此错误消息:

After 1018 times looping (this number doesn't change no matter how many times I try), I get this error message:

SQLSTATE[HY000]: General error: 14 unable to open database file

我检查了这里写的解决方案: 如何防止SQLITE SQLSTATE [HY000] [14]? 但是回显的消息仅在循环的第一次出现,因此我认为PDO连接没有关闭.

I checked the solution written here: How to prevent SQLITE SQLSTATE[HY000] [14]? but the echoed message only appears at the first time of the loop, so I assume the PDO connection isn't closed.

我没有找到与此错误代码相关的其他文档.

I didn't find other documentation relevant to this error code.

这里可能出什么问题了?

What could possibly go wrong here?

我试图按以下条件移动prepare和bind命令.不会引发异常,但是仅保存第一个图块(或不确定每个图块都保存在第一个图块的顶部):

I tried to move the prepare and bind commands in a condition as follows. The exception isn't raised, but only the first tile is saved (or every tile is saved on top of the first one, not sure):

if ($pdo_mbtiles == null) {
    echo "Opening new database connection".PHP_EOL;
    $pdo_mbtiles = new PDO('sqlite:'.$filename,
            '',
            '',
            array(
                PDO::ATTR_PERSISTENT => true
                )
            );
    $pdo_mbtiles->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    $pdo_mbtiles->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}

if ($q == null) {
    $q = $pdo_mbtiles->prepare("INSERT INTO tiles (zoom_level, tile_column, tile_row,tile_data) VALUES (:zoom_level, :tile_column, :tile_rowTMS, :tile_data)");
    $q->bindParam(':zoom_level', $zoom_level);
    $q->bindParam(':tile_column', $tile_column);
    $q->bindParam(':tile_rowTMS', $tile_rowTMS);
    $q->bindParam(':tile_data', $tile_data, PDO::PARAM_LOB);
}
$q->execute();

以下是生成过程中的文件:

Here are the files during the generation:

在引发异常之后,这里是:

And here after the exception is raised:

此外,当引发异常时,我对我的$ pdo_mbtiles进行了var_dump转换,结果如下(与成功执行时完全相同):

Also, when the exception is raised, I do a var_dump of my $pdo_mbtiles, and here's the result (exactly the same as when I do it with a success):

object(PDO)#116 (0) {
}

仍在尝试解决此问题,我简化了创建MBTiles文件的代码.尚无成功,但是如果有人想要重现此问题,请参考以下示例.您可以从 https://www.dropbox.com/s下载/33vqamc9tn4c3ux/sample_PHP_MBTiles_generation_bug.zip?dl=0

Still trying to solve this problem, I simplified the code to create the MBTiles file. No success yet, but here's a sample if anyone want's to reproduce the issue. You can download it from https://www.dropbox.com/s/33vqamc9tn4c3ux/sample_PHP_MBTiles_generation_bug.zip?dl=0

推荐答案

该错误消息具有误导性.经过许多小时的调试,我发现它与我的数据库连接完全无关. 只是我使用fopen()来获取切片数据,而在注册后没有使用fclose(),从而达到了1024的限制.

The error message was misleading. After many hours of debugging, I found that it was completely unrelated to my database connection. It's juste that I used fopen() to get tiles data, and didn't fclose() after registration, thus reaching the limit of 1024.

1024是因为我使用了六个require或require_once语句,所以1018个瓦片请求+ 6个require = 1024个打开的连接.

1024 is because I used six require or require_once statements, so 1018 tile requests + 6 require = 1024 opened connections.

这篇关于经过多次操作后,来自PHP的SQL连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆