【发布时间】:2014-04-04 17:00:21
【问题描述】:
试图从数据库中的表返回结果。 此代码有效:
| connection results |
PGConnection defaultConnectionArgs
hostname: '*****.awebservice.com';
portno: 1234;
databaseName: '*****';
userName: '********';
password: '**************'.
connection := PGConnection new.
connection startup.
results := connection execute: 'SELECT uid, code, name FROM public.Project'.
Transcript show: results rows.
但在分析它时,我想知道如何访问单行。在 Smalltalk 类中,ROWS 是什么?
如果我使用此代码对返回的OrderedCollection 进行进一步处理(结果):
results rows do: [:row | | data |
data := row dataKeyedByFieldName.
Transcript show: 'Uid: ''', (data at: 'uid') , ''''; cr.
Transcript show: 'Code: ''', (data at: 'code') , ''''; cr.
Transcript show: 'Name: ''', (data at: 'name') , ''''; cr; cr].
connection terminate.
我收到一个错误:Instances of SmallInteger are not indexable。
我会坦白地说我是从网上得到的,但我在任何地方都找不到 ROWS 或 DATAKEYEDBYFIELDNAME 作为任何 Smalltalk 类中的方法。
编辑:
好的,我发现 connection: execute 的返回变量是PGAsciiRow 类型,#dataKeyedByFieldName 方法是通过使用名称索引而不是简单的数字来启用迭代。但仍然不知道错误来自哪里。
以下是堆栈跟踪:
errorNotIndexable
"Create an error notification that the receiver is not indexable."
self error: ('Instances of {1} are not indexable'
translated format: {self class name})
【问题讨论】:
-
Pharo 5.0 也许是 3.0?
-
Pharo 3.0 比 Pharo 5.0 好?
-
没有 Pharo 5。当前稳定版是 2.0,即将发布的是 3.0(请参阅pharo-project.org/home)
-
Instances of SmallInteger are not indexable表示您正在尝试向SmallInteger发送#at:或类似消息。在没有看到堆栈的情况下,我们几乎无能为力。尝试在调试器中右键单击堆栈帧条目并将堆栈复制到剪贴板,然后将其粘贴到此处(至少前 20 行左右)。 -
errorNotIndexable "创建接收器不可索引的错误通知。"自我错误:(“{1} 的实例不可索引”翻译格式:{self 类名})