JDD/Methods/Read

From J Wiki
< JDD
Jump to: navigation, search
JDD: Driver Locale | Handles | Data Sources | Data Driver | Error Messages | API: Connect Read Update Create Bulk Insert

To read data from a file, you first select the data you want to read using the ddsel verb, which returns a statement handle. You then use ddfet or ddfch to fetch the records.

The left argument of ddsel is a SQL selection expression. Here are typical examples:

Select all records (* means all columns):

   sh=. 'select * from tdata' ddsel__db ch

Fetch the first 3 records:

   ddfet__db sh,3
+-----------+-+----+----------+----------+-----++
|Macdonald B|F|D101|1959-06-01|1978-05-01|32591||
+-----------+-+----+----------+----------+-----++
|Genereaux S|F|D103|1945-03-01|1966-02-01|95415||
+-----------+-+----+----------+----------+-----++
|Koebel R   |M|D101|1937-11-01|1980-09-01|63374||
+-----------+-+----+----------+----------+-----++

Fetch the next record:

   ddfet__db sh
+--------+-+----+----------+----------+-----++
|Keller J|F|D101|1951-05-01|1974-04-01|48898||
+--------+-+----+----------+----------+-----++

Close the statement handle:

   ddend__db sh

You should always close the statement handle when you no longer need it. However to avoid repetition, the remaining examples do not show this.

Select males with salary exceeding 40000:

   sel=.'select * from tdata where sex=''M'' and salary >= 40000'
   sh=. sel ddsel__db ch
   ddfet__db sh,4
+----------+-+----+----------+----------+------++
|Koebel R  |M|D101|1937-11-01|1980-09-01|63374 ||
+----------+-+----+----------+----------+------++
|Newton R  |M|D108|1956-01-01|1979-02-01|73368 ||
+----------+-+----+----------+----------+------++
|Dingee S  |M|D103|1964-10-01|1983-09-01|46877 ||
+----------+-+----+----------+----------+------++
|Rogerson G|M|D101|1957-12-01|1983-02-01|108777||
+----------+-+----+----------+----------+------++

Select only the name, department and salary fields, where date of birth is before 1950:

   sel=.'select name,dept,salary from tdata where dob<''1950-01-01'''

Fetch the first such record:

    ddfet__db (sel ddsel__db ch),1

+-----------+----+-----+
|Genereaux S|D103|95415|
+-----------+----+-----+

Use ddfch to return data in columns:

   sh=. sel ddsel__db ch
   [a=. ddfch__db sh,_1
+-----------+----+-----+
|Genereaux S|D103|95415|
|Koebel R   |D101|63374|
|Denny D    |D101|46939|
|Bugg P     |D101|47165|
|Anctil J   |D108|60974|
|O'Keefe D  |D101|66377|
|Cahill G   |D108|81358|
+-----------+----+-----+