使用 Perl DBI selectall_hashref 时出现错误 "attribute parameter is not in hash ref"
Error "attribute parameter is not in hash ref" when using Perl DBI selectall_hashref
我正在尝试使用 Perl DBI 模块中的 selectall_hashref
让我的第一个 select 工作。我已成功打开与数据库 (MySQL) 的连接。执行以下命令时出现错误:
$dbh->selectall_hashref('SELECT id FROM users WHERE login=?',undef,"myusername");
DBI::st=HASH(0x1505a60)->_prepare(...): attribute parameter 'myusername' is not a hash ref at /usr/lib/x86_64-linux-gnu/perl5/5.20/DBD/mysql.pm line 238.
我的 table 应该能够支持这个查询,它有一个 id
列和每个用户的 login
列。
我为 selectall_hashref
找到的示例显示 ?
替换参数作为第三个参数传递。 DBI documentation 说第二个和第三个参数应该是 %attr
和 @bind_values
但没有提供太多关于它们的文档或显示工作示例。
是什么导致了错误,更重要的是您如何正确使用 %attr
和 @bind_values
?
这些方法需要键列作为第二个参数,属性 ref 作为第三个参数传递。在结果中,它构建了一个以指定列为键的散列。您可能想要的是 selectall_arrayref
:
$ dbh->selectall_arrayref('SELECT id FROM users WHERE login=?',undef,"myusername");
如果你想将所有内容存储为 arrayref,其中每一行都是一个 hashref(这就是你的 似乎表明的),你可以使用 selectall_arrayref()
方法和 Slice
属性:
$dbh->selectall_arrayref('SELECT id FROM users WHERE login=?', {Slice => {}}, 'myusername');
有点奇怪,但是here's how it works:
If $slice is a hash reference, fetchall_arrayref
fetches each row as
a hash reference. If the $slice hash is empty then the keys in the
hashes have whatever name lettercase is returned by default. (See
"FetchHashKeyName" attribute.) If the $slice hash is not empty,
then it is used as a slice to select individual columns by name. The
values of the hash should be set to 1. The key names of the returned
hashes match the letter case of the names in the parameter hash,
regardless of the "FetchHashKeyName" attribute.
最好在数据库句柄上设置 FetchHashKeyName
属性以使您的哈希键名称保持一致;我碰巧在我的应用程序中喜欢 NAME_lc
。
我正在尝试使用 Perl DBI 模块中的 selectall_hashref
让我的第一个 select 工作。我已成功打开与数据库 (MySQL) 的连接。执行以下命令时出现错误:
$dbh->selectall_hashref('SELECT id FROM users WHERE login=?',undef,"myusername");
DBI::st=HASH(0x1505a60)->_prepare(...): attribute parameter 'myusername' is not a hash ref at /usr/lib/x86_64-linux-gnu/perl5/5.20/DBD/mysql.pm line 238.
我的 table 应该能够支持这个查询,它有一个 id
列和每个用户的 login
列。
我为 selectall_hashref
找到的示例显示 ?
替换参数作为第三个参数传递。 DBI documentation 说第二个和第三个参数应该是 %attr
和 @bind_values
但没有提供太多关于它们的文档或显示工作示例。
是什么导致了错误,更重要的是您如何正确使用 %attr
和 @bind_values
?
这些方法需要键列作为第二个参数,属性 ref 作为第三个参数传递。在结果中,它构建了一个以指定列为键的散列。您可能想要的是 selectall_arrayref
:
$ dbh->selectall_arrayref('SELECT id FROM users WHERE login=?',undef,"myusername");
如果你想将所有内容存储为 arrayref,其中每一行都是一个 hashref(这就是你的 selectall_arrayref()
方法和 Slice
属性:
$dbh->selectall_arrayref('SELECT id FROM users WHERE login=?', {Slice => {}}, 'myusername');
有点奇怪,但是here's how it works:
If $slice is a hash reference,
fetchall_arrayref
fetches each row as a hash reference. If the $slice hash is empty then the keys in the hashes have whatever name lettercase is returned by default. (See "FetchHashKeyName" attribute.) If the $slice hash is not empty, then it is used as a slice to select individual columns by name. The values of the hash should be set to 1. The key names of the returned hashes match the letter case of the names in the parameter hash, regardless of the "FetchHashKeyName" attribute.
最好在数据库句柄上设置 FetchHashKeyName
属性以使您的哈希键名称保持一致;我碰巧在我的应用程序中喜欢 NAME_lc
。