Perl 使用 dbi 的一个简单错误
我的准备语句
$sqlst = $dbh->prepare('SELECT * FROM starter_trot WHERE UserId = 2345' ) or die "Couldn't prepare statement: " . $dbh->errstr;
$sqlst->execute($userid) or die "Couldn't execute statement: " . $sqlst->errstr;
my @data;
print"hai";
while (@data = $sqlst->fetchrow_array())
{
print "**";
}
执行语句中有一个错误,并且准备语句肯定不会失败。
[WHERE UserId = 2345]这是失败的部分。当我在数据库中运行查询时,它会重新调整值。但是当我通过脚本运行查询时,它会失败(但没有编译或运行时问题)问题是什么。是吗?在准备中我们必须给出?(绑定变量而不是实际值?) 〜 〜
I have a error in my prepare statement
$sqlst = $dbh->prepare('SELECT * FROM starter_trot WHERE UserId = 2345' ) or die "Couldn't prepare statement: " . $dbh->errstr;
$sqlst->execute($userid) or die "Couldn't execute statement: " . $sqlst->errstr;
my @data;
print"hai";
while (@data = $sqlst->fetchrow_array())
{
print "**";
}
execute statement and prepare statement does not fail for sure.
[WHERE UserId = 2345]This is the part it fails.when i run the query in the db it retuns values.But when i run the query through scripts it fails (But no compilation or runtime issues)what is the problem.Is it in prepare we have to give with ?(bind variables and not actual values?)
~
~
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用严格,使用警告,使用 DBI 时使用 RaiseError。当 SQL 语句中没有占位符时,您正在使用一个绑定值执行。当然,您应该按照自己的方式看到错误消息(因为 PrintError 是默认值),但是 RaiseError 比到处撒上“or die ...”更容易。
use strict, use warnings, and when using DBI, use RaiseError. You are executing with one bind value, when you have no placeholders in your SQL statement. Sure, you should see the error message the way you have it (since PrintError is the default), but RaiseError is easier than sprinkling 'or die ...' everywhere.
数据库可能会返回大写的列名。尝试一下:
我打赌它会起作用。
The DB is likely returning the column name as upper case. Try:
and I bet it will work.