Forum

Kognitio Console issues, advice, etc.
Contributor
Offline
Posts: 184
Joined: Wed May 29, 2013 2:10 pm
Location: Bracknell

Columns in the kogscript Lua table returned by a SQL query

by MikeAtkinson » Thu Oct 27, 2016 3:50 pm

The returned Lua table has a set of rows, each of these rows contain the column data twice, indexed both by the column number and by the column name.

Code: Select all

t = select 1 as one;

print(t.rows[1].ONE); -- 1
print(t.rows[1][1]);   -- “1”

t = select 2 as “two”;

print(t.rows[1].two); -- 2
print(t.rows[1][1]);   -- “2”

t = select 3 as "3rd";

print(t.rows[1]["3rd"]); -- 3
print(t.rows[1][1]);      -- “3”

Note that the column name is case sensitive and is that of the SQL column. When indexed by the column name the column data is its natural number, string, date, etc. representation; when indexed by the column number the column data is its string representation.
Note that dates and times are special; their natural representation is as a Lua table:

Code: Select all

t = select DATE '2016-10-27' as today;

print(t.rows[1].TODAY.year);   -- 2016
print(t.rows[1].TODAY.month); -- 10
print(t.rows[1].TODAY.day);    -- 27
print(t.rows[1][1]);                -- "2016-10-27"

t = select TIME  '14:20:15' as time_now;

print(t.rows[1].TIME_NOW.hour);  -- 14
print(t.rows[1].TIME_NOW.min);   -- 20
print(t.rows[1].TIME_NOW.sec);   -- 15
print(t.rows[1][1]);                    -- "14:20:15"
Reply with quote Top

Who is online

Users browsing this forum: No registered users and 1 guest

cron