Pharo 2.0 Smalltalk和PostgresV2

时间:2014-04-04 17:00:21

标签: smalltalk pharo

尝试从数据库中的表返回结果。 此代码有效:

| 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。 我会干净利落地说我从网上得到了这个,但在任何Smalltalk类中我都无法找到ROWS或DATAKEYEDBYFIELDNAME作为方法。

编辑: 好的我发现来自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})

1 个答案:

答案 0 :(得分:1)

最有可能

data at: 'uid'

(或'code')正在回答一个整数。然后,这一行:

Transcript show: 'Uid: ''', (data at: 'uid') , ''''; cr.

应该写得更好:

Transcript show: 'Uid: ''', (data at: 'uid') asString, ''''; cr.

类似的东西:)