Reputation: 54
Trying to return results from a table in a DB. This code works:
| 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.
But in analysing it I want to know how to access a single row. Where in Smalltalk classes is ROWS?
If I do further processing of the OrderedCollection
returned (results) with this code:
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.
I get an error: Instances of SmallInteger are not indexable
.
I will come clean and say I got this off the web but nowhere can I find either ROWS or DATAKEYEDBYFIELDNAME as methods in any Smalltalk class.
Edit:
OK I found out that the return variable from connection: execute is of type PGAsciiRow
and the method #dataKeyedByFieldName
is to enable iteration by using name index instead of simple numeric. But still don't know where the error is coming from.
The following is a stack trace:
errorNotIndexable
"Create an error notification that the receiver is not indexable."
self error: ('Instances of {1} are not indexable'
translated format: {self class name})
Upvotes: 1
Views: 635
Reputation: 4357
most probably
data at: 'uid'
(or 'code') is answering an integer number. Then, this line:
Transcript show: 'Uid: ''', (data at: 'uid') , ''''; cr.
should better be written:
Transcript show: 'Uid: ''', (data at: 'uid') asString, ''''; cr.
something like that :)
Upvotes: 1