不明白DBD :: MySQL属性“mysql_bind_type_guessing”的行为

时间:2013-02-11 10:38:20

标签: mysql perl dbi

为什么在启用mysql_bind_type_guessing时出现此错误,为什么只有参数“e”而不是参数“a”?

#!/usr/bin/env perl
use warnings;
use strict;
use 5.10.1;
use utf8;
use open qw(:utf8 :std);
use DBI;

my $user = '...';
my $passwd = '...';

my $dbh = DBI->connect( "DBI:mysql:dbname=information_schema", $user, $passwd, {
    PrintError => 0,
    RaiseError => 1,
} ) or die DBI->errstr;

my $db = 'my_test_db';
$dbh->do( "DROP DATABASE IF EXISTS $db" );
$dbh->do( "CREATE DATABASE $db" );
$dbh = DBI->connect( "DBI:mysql:dbname=$db", $user, $passwd, {
    PrintError => 0,
    RaiseError => 1,
    AutoCommit => 1,
    mysql_enable_utf8 => 1,
    mysql_bind_type_guessing => 1,
} ) or die DBI->errstr;

my $table = 'Abteilung';
$dbh->do( "CREATE TABLE IF NOT EXISTS $table (
AbtNr      INT NOT NULL,
Name       VARCHAR(30),
PRIMARY KEY(AbtNr)
)" );

my $sth = $dbh->prepare( "INSERT INTO $table ( AbtNr, Name ) VALUES ( ?, ? )" );
my $abteilung_values = [
    [ 1, 'EDV'        ],
    [ 2, 'Verwaltung' ],
    [ 3, 'Chefetage'  ],
];
for my $row ( @$abteilung_values ) {
    $sth->execute( @$row );
}

$sth = $dbh->prepare( "SELECT * FROM $table WHERE name REGEXP ?" );
$sth->execute( 'e' );
while ( my $row = $sth->fetchrow_arrayref() ) {
    say "@$row";
}

mysql_bind_type_guessing enabled

execute argument 'e':
# DBD::mysql::st execute failed: Unknown column 'e' in 'where clause' at ./perl3.pl line 46.

execute argument 'a':
# 2 Verwaltung
# 3 Chefetage


mysql_bind_type_guessing disabled

execute argument 'e':
# 1 EDV
# 2 Verwaltung
# 3 Chefetage

execute argument 'a':
# 2 Verwaltung
# 3 Chefetage

1 个答案:

答案 0 :(得分:1)

参数mysql_bind_type_guessing会尝试猜测您的输入数据是否为数字。如果数据是数字,则不会引用它。

似乎对数字的检查不充分导致它认为e是一个数字(e可以是数字的一部分,例如1.2e-3)。< / p>

因为没有引用,MySql会尝试将e视为列名,而是错误。

如果我的猜测是正确的,我会认为它是DBD::MySQL中的错误。

更新: Indeed, this is a bug