DBD :: CSV和占位符

sid*_*com 3 sql csv perl placeholder dbi

#!/usr/bin/env perl
use warnings;
use strict;
use DBI;

my $dbh = DBI->connect( "DBI:CSV:", '', '', { RaiseError => 1 } ) or die DBI->errstr;

my $table = 'my_test_table_1.csv';
$dbh->do( "CREATE TEMP TABLE $table( id INTEGER, name CHAR(64) )" );

my $sth = $dbh->prepare( "INSERT INTO $table ( id, name ) VALUES( ?, ? )" );
$sth->execute( 1, 'Ruth' );
$sth->execute( 2, 'John' );
$sth->execute( 3, 'Nena' );
$sth->execute( 4, 'Mark' );

$sth = $dbh->prepare( "SELECT * FROM $table WHERE id > ? LIMIT ?" );
$sth->execute( 1, 2 );
$sth->dump_results;

# Bad limit clause! at /usr/local/lib/perl5/site_perl/5.20.1/SQL/Statement.pm line 88.
Run Code Online (Sandbox Code Playgroud)

它看起来像LIMIT子句中的占位符不起作用.

当我使用DBD :: CSV驱动程序时,如何判断SQL语句中某个位置是否支持占位符?

ike*_*ami 5

占位符只能在需要表达式的地方使用.以下内容LIMIT必须是行计数(不是表达式),因此它不能是占位符.

#!/usr/bin/perl
use warnings;
use strict;
use DBI qw( );

my $dbh = DBI->connect("DBI:CSV:", undef, undef, {
   PrintError => 0, RaiseError => 1,
} );

{
   my $sth = $dbh->prepare( "SELECT 1 LIMIT 1" );
   $sth->execute();
   $sth->finish();
   print "ok\n";
}

{
   my $sth = $dbh->prepare( "SELECT 1 LIMIT 0+1" );
   $sth->execute();
   $sth->finish();
   print "ok\n";
}
Run Code Online (Sandbox Code Playgroud)

ok
DBD::CSV::db prepare failed: Bad limit clause! at .../SQL/Statement.pm line 88.
 [for Statement "SELECT 1 LIMIT 0+1"] at a.pl line 18.
Run Code Online (Sandbox Code Playgroud)

  • 情况并非总是如此。我认为 DBD::mysql(而不是数据库服务器)当前正在内联占位符(这意味着准备操作实际上是在 `execute` 期间完成的)。如果是这样,服务器永远不会看到占位符,只会看到“LIMIT 1”,因此它不知道您犯了语法错误。(例如`SET @FOO = 1; SELECT 1 LIMIT @FOO;` 失败。)DBD::CSV 根本不会为你做所有的魔法。 (2认同)