Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 我的Mysql DBD insert调用突然悄无声息地失败_Perl - Fatal编程技术网

Perl 我的Mysql DBD insert调用突然悄无声息地失败

Perl 我的Mysql DBD insert调用突然悄无声息地失败,perl,Perl,使用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,

使用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构建的Perl5,版本14,Subversion1V5.14.1

注意:此语法正常工作:

$dbh->doq/插入到call_字段值ll,0,0,testing/或die$dbh::errstr

您的打印错误:

... or die print "er $DBI::errstr";
看起来不对

正如Alan所提到的,您修复了双冒号,但是对正在执行的die的打印调用返回1。1==成功打印…某处

将模具语法更改为:

... or die "er $DBI::errstr";
你应该得到这样的东西:

er {DBI_ERROR_MESSAGE} at {Script_Name} line XXX.
你写道:

$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";
还有一个例子:

$dbh->do(q/insert into call_fields values(null,0,0,"testing") /) or die "$dbh::errstr";
这是一个苹果和桔子的比较。第一个是插入字符串NULL,但第二个是插入“NULL”值

我假设获取第一个准备好的输入值的call\u fields表不接受字符串,因此$sth->execute失败。

语句

die print "$DBI:errstr";
这是可疑的。正如Alan Haggai Alavi在评论中指出的,您缺少一个冒号。它应该是$DBI::errstr。另外,它在stdERR而不是stdout中显示1,因为您使用打印链接模具。打印成功返回1,返回模具,然后显示

它还应该显示print-in-stdout,或者至少显示print中未定义值的警告,但是除非您使用use-warnings,否则它将相当安静。它可能会打印:errstr

因此:

应该会改善你的处境。添加

use strict;
use warnings;
如果还没有,它将进一步改进。

设置并让DBI进行错误处理

Perl中的字符串,而不是带引号的字符串NULL


希望您在整理所有提供给您的信息时有点困惑,这里是您的代码重写,以使用您提供的所有建议以及我所做的一些其他更改

我已经评论了我在每一行上所做的更改

# Declared $dbh
# Removed unnecessary quote marks.
my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost", $dbuser, $dbpass);

# Removed unnecessary call to print
# Replaced $DBI::errstr with $sth->errstr
my $sth = $dbh->prepare( 'INSERT INTO call_fields VALUES ( ?, ?, ?, ? )' )
  or die $sth->errstr;

# Replaced string "NULL" with undef (which DBI treats as NULL)
# Removed unnecessary call to print
# Replaced $DBI::errstr with $sth->errstr
$sth->execute( undef, 0, 0, 'testing' ) or die $sth->errstr;
我怀疑是删除了不必要的打印调用,并将NULL切换为undef,才真正解决了您的问题


此外,我强烈建议在连接DB和b时设置RaiseError标志,将列名添加到INSERT语句,然后删除第一个可为null的,语句中的所有列。

对这些未提及的列有任何限制吗?$DBI:errstr应替换为$DBI::errstr,这样静默将消失:-它在stdout中只显示1第一个字段是autoincrement,其他字段没有任何特殊功能您可以尝试手动执行查询吗?为什么这样做可以:$dbh->doq/insert-into-call_-fields-valuesnall,0,0,testing/or-die$dbh::errstr@tom可能是因为第一个字段将接受null值,而不是像您之前添加的那样接受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');
# Declared $dbh
# Removed unnecessary quote marks.
my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost", $dbuser, $dbpass);

# Removed unnecessary call to print
# Replaced $DBI::errstr with $sth->errstr
my $sth = $dbh->prepare( 'INSERT INTO call_fields VALUES ( ?, ?, ?, ? )' )
  or die $sth->errstr;

# Replaced string "NULL" with undef (which DBI treats as NULL)
# Removed unnecessary call to print
# Replaced $DBI::errstr with $sth->errstr
$sth->execute( undef, 0, 0, 'testing' ) or die $sth->errstr;