Search code examples
pharo

SQLite3Cursor object: how to access row values


I am fairly new to Pharo and trying hard to get a grip of it.

Installed the SQLite3 package and now I am trying to connect to local file based database.

I followed the Getting started tutorial of the community owned SQLite3 database client. Unfortunately only brief documentation is provided.

Can someone give me an example how to iterate through the SQLite3Cursor object and print them e.g. to the Transcript please?

Secondly I would like to know how I am able to access certain row values.

Appreciate any help for a newbie. Thank you.


If I evaluate

cursor := connection execute: 'SELECT * FROM person;'

All persons are put in that cursor object. Basically I get n SQLite3Rows within that cursor. If I inspect cursor next I see the columns and values of that row but how can I display it in Transcript?

Second question is how can I iterate through the entire cursor object and send the output to Transcript?


Solution

  • Basicly you can do like this

    | conn rs rows read|
    conn := SQLite3Connection memory.
    conn open.
    
    conn execute: 'CREATE TABLE person(
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        name TEXT NOT NULL,
        age INTEGER NOT NULL
    );'.
    
    conn execute: 'INSERT INTO person(name,age) VALUES (?2, ?1);' value: 25 value: 'Cyril'.
    
    conn execute: 'INSERT INTO person(name,age) VALUES (?2, ?1);' value: 24 value: 'Marc'.
    
    rs := conn execute: 'SELECT * FROM person;'.
    
    """ OrderedCollection """
    rows := rs rows.
    
    rows do: [ :v |
        Transcript show: (v at: 'name'); cr.
         ]
    

    The best is to look in SQLite3-Core-Tests