Skip to main content
 首页 » 编程设计

perl之我的 Mysql DB 插入调用突然无声地失败

2025年05月04日86fff_TT

使用 DBI();

导致此插入失败的原因 1,SELECT 调用工作正常,因此它不是凭据问题。

$dbh = DBI->connect("DBI:mysql:$dbname:$dbhost","$dbuser","$dbpass"); 
my $sth = $dbh->prepare( "INSERT INTO call_fields VALUES ( ?, ?, ?, ? )" ) or die print "$DBI:errstr"; 
$sth->execute( "NULL", 0, 0, "testing" ) or die print "er $DBI::errstr"; 

mysql 版本 5.5 这是为 MSWin32-x64 构建的 perl 5,版本 14,subversion 1 (v5.14.1)

注意:此语法可以正常工作:

$dbh->do(q/insert into call_fields values(null,0,0,"testing")/) or die "$dbh::errstr";

请您参考如下方法:

设置RaiseError connection attribute并让 DBI 进行错误处理。

SQL NULL value is represented as undef在 Perl 中,不是带引号的字符串 NULL

use strictures; 
use DBI qw(); 
my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost", $dbuser, $dbpass, { RaiseError => 1 }); 
my $sth = $dbh->prepare('INSERT INTO call_fields VALUES ( ?, ?, ?, ? )'); 
$sth->execute(undef, 0, 0, 'testing');