Table creation: Difference between revisions

m
Rearranged some language entries into alphabetical order.
m (→‎{{header|Go}}: Corrected typo in comment.)
m (Rearranged some language entries into alphabetical order.)
Line 56:
}
</lang>
 
=={{header|J}}==
 
If we define a <code>table</code> as a named collection of columns, and we define a <code>type</code> as a mechanism for the representation of some kind of data, then:
 
<lang j>stocks=: |: ,: ;:'date trans symbol qty price'
insertStock=: 3 :'0#stocks=: stocks,.y'
insertStock@".;._2]0 :0
'2006-01-05'; 'BUY'; 'RHAT'; 100; 35.14
'2006-03-28'; 'BUY'; 'IBM'; 1000; 45.00
'2006-04-05'; 'BUY'; 'MSOFT'; 1000; 72.00
'2006-04-06'; 'SELL'; 'IBM'; 500; 53.00
)</lang>
 
declares a table and some data within that table.
 
And, here's an example of sorting:
 
<lang j>cols=: [:; {."1@[ <@i.`(<@i.@#@[)@.(=&(<,'*')@]"1 0) cutopen@]
sortBy=: [ /:"1 2 (<__)|:@,. [ }.@{~ cols
from=: cols~ {"0 _ ]
select=: |:
 
select '*' from stocks sortBy 'price'
┌──────────┬─────┬──────┬────┬─────┐
│date │trans│symbol│qty │price│
├──────────┼─────┼──────┼────┼─────┤
│2006-01-05│BUY │RHAT │100 │35.14│
├──────────┼─────┼──────┼────┼─────┤
│2006-03-28│BUY │IBM │1000│45 │
├──────────┼─────┼──────┼────┼─────┤
│2006-04-06│SELL │IBM │500 │53 │
├──────────┼─────┼──────┼────┼─────┤
│2006-04-05│BUY │MSOFT │1000│72 │
└──────────┴─────┴──────┴────┴─────┘</lang>
 
Note that this particular example is both overly general in some senses (for example, named column handling has features not demonstrated here) and overly specific in others (for example, I did not implement sort in descending order).
 
Also, a properly tuned system would likely use different code (for example, you could get better performance if you put an entire column into a box instead of introducing a new box for each element in a column).
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<lang julia>using SQLite
 
conn = SQLite.DB() # in-memory
SQLite.execute!(conn, """
create table stocks
(date text, trans text, symbol text,
qty real, price real)
""")
 
# Insert a row of data
SQLite.execute!(conn, """
insert into stocks
values ('2006-01-05','BUY','RHAT',100,35.14)
""")
 
for v in [["2006-03-28", "BUY", "IBM", 1000, 45.00],
["2006-04-05", "BUY", "MSOFT", 1000, 72.00],
["2006-04-06", "SELL", "IBM", 500, 53.00]]
SQLite.query(conn, "insert into stocks values (?,?,?,?,?)", values = v)
end
 
df = SQLite.query(conn, "select * from stocks order by price")
println(df)</lang>
 
{{out}}
<pre>4×5 DataFrames.DataFrame
│ Row │ date │ trans │ symbol │ qty │ price │
├─────┼──────────────┼────────┼─────────┼────────┼───────┤
│ 1 │ "2006-01-05" │ "BUY" │ "RHAT" │ 100.0 │ 35.14 │
│ 2 │ "2006-03-28" │ "BUY" │ "IBM" │ 1000.0 │ 45.0 │
│ 3 │ "2006-04-06" │ "SELL" │ "IBM" │ 500.0 │ 53.0 │
│ 4 │ "2006-04-05" │ "BUY" │ "MSOFT" │ 1000.0 │ 72.0 │</pre>
 
=={{header|FunL}}==
Line 279 ⟶ 204:
4 2006-04-05 BUY MSOFT 1000 72.00 false
</pre>
 
=={{header|J}}==
 
If we define a <code>table</code> as a named collection of columns, and we define a <code>type</code> as a mechanism for the representation of some kind of data, then:
 
<lang j>stocks=: |: ,: ;:'date trans symbol qty price'
insertStock=: 3 :'0#stocks=: stocks,.y'
insertStock@".;._2]0 :0
'2006-01-05'; 'BUY'; 'RHAT'; 100; 35.14
'2006-03-28'; 'BUY'; 'IBM'; 1000; 45.00
'2006-04-05'; 'BUY'; 'MSOFT'; 1000; 72.00
'2006-04-06'; 'SELL'; 'IBM'; 500; 53.00
)</lang>
 
declares a table and some data within that table.
 
And, here's an example of sorting:
 
<lang j>cols=: [:; {."1@[ <@i.`(<@i.@#@[)@.(=&(<,'*')@]"1 0) cutopen@]
sortBy=: [ /:"1 2 (<__)|:@,. [ }.@{~ cols
from=: cols~ {"0 _ ]
select=: |:
 
select '*' from stocks sortBy 'price'
┌──────────┬─────┬──────┬────┬─────┐
│date │trans│symbol│qty │price│
├──────────┼─────┼──────┼────┼─────┤
│2006-01-05│BUY │RHAT │100 │35.14│
├──────────┼─────┼──────┼────┼─────┤
│2006-03-28│BUY │IBM │1000│45 │
├──────────┼─────┼──────┼────┼─────┤
│2006-04-06│SELL │IBM │500 │53 │
├──────────┼─────┼──────┼────┼─────┤
│2006-04-05│BUY │MSOFT │1000│72 │
└──────────┴─────┴──────┴────┴─────┘</lang>
 
Note that this particular example is both overly general in some senses (for example, named column handling has features not demonstrated here) and overly specific in others (for example, I did not implement sort in descending order).
 
Also, a properly tuned system would likely use different code (for example, you could get better performance if you put an entire column into a box instead of introducing a new box for each element in a column).
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<lang julia>using SQLite
 
conn = SQLite.DB() # in-memory
SQLite.execute!(conn, """
create table stocks
(date text, trans text, symbol text,
qty real, price real)
""")
 
# Insert a row of data
SQLite.execute!(conn, """
insert into stocks
values ('2006-01-05','BUY','RHAT',100,35.14)
""")
 
for v in [["2006-03-28", "BUY", "IBM", 1000, 45.00],
["2006-04-05", "BUY", "MSOFT", 1000, 72.00],
["2006-04-06", "SELL", "IBM", 500, 53.00]]
SQLite.query(conn, "insert into stocks values (?,?,?,?,?)", values = v)
end
 
df = SQLite.query(conn, "select * from stocks order by price")
println(df)</lang>
 
{{out}}
<pre>4×5 DataFrames.DataFrame
│ Row │ date │ trans │ symbol │ qty │ price │
├─────┼──────────────┼────────┼─────────┼────────┼───────┤
│ 1 │ "2006-01-05" │ "BUY" │ "RHAT" │ 100.0 │ 35.14 │
│ 2 │ "2006-03-28" │ "BUY" │ "IBM" │ 1000.0 │ 45.0 │
│ 3 │ "2006-04-06" │ "SELL" │ "IBM" │ 500.0 │ 53.0 │
│ 4 │ "2006-04-05" │ "BUY" │ "MSOFT" │ 1000.0 │ 72.0 │</pre>
 
=={{header|Lua}}==
Line 490:
printf(1,"sqlite3_exec error: %d [%s]\n",{res,sqlite_last_exec_err})
end if</lang>
 
=={{header|PL/I}}==
<lang PL/I>declare 1 table (100),
2 name character (20) varying,
2 address,
3 number fixed decimal,
3 street character (30) varying,
3 suburb character (30) varying,
3 zip picture '9999',
2 transaction_date date,
2 sex character (1),
2 suppress_junk_mail bit (1);</lang>
 
=={{header|PicoLisp}}==
Line 543 ⟶ 531:
12345 20090513 No John Doe 77.22
12346 20090514 Yes Jane Miller 123.75</pre>
 
=={{header|PL/I}}==
<lang PL/I>declare 1 table (100),
2 name character (20) varying,
2 address,
3 number fixed decimal,
3 street character (30) varying,
3 suburb character (30) varying,
3 zip picture '9999',
2 transaction_date date,
2 sex character (1),
2 suppress_junk_mail bit (1);</lang>
 
=={{header|PostgreSQL}}==
9,477

edits