Addons/data/sqlite/Basic Usage
Overview | Basic Usage | Create | Definitions | zFns | Installation | Nulls | Examples | Quirks
Overview
The addon is read into locale psqlite. A sqlite database is opened or created in an instance of this locale, and subsequent access uses this instance.
For example, using the sandp database included with the addon:
load 'data/sqlite' db=: sqlopen_psqlite_ '~addons/data/sqlite/db/sandp.db'
Meta Data
Tables in database:
sqltables__db'' βββ¬ββ¬βββ βpβsβspβ βββ΄ββ΄βββ
Table description:
sqlmeta__db 's' βββββ¬βββββββ¬βββββ¬ββββββββ¬βββββββββββ¬βββ βcidβname βtypeβnotnullβdflt_valueβpkβ βββββΌβββββββΌβββββΌββββββββΌβββββββββββΌβββ€ β0 βsid βtextβ0 β β1 β β1 βname βtextβ0 β β0 β β2 βstatusβint β0 β β0 β β3 βcity βtextβ0 β β0 β βββββ΄βββββββ΄βββββ΄ββββββββ΄βββββββββββ΄βββ
Reading Data
The main function is sqlread which returns a pair: column names, list of column_values. There are several other reading functions that call sqlread under the covers.
sqlread__db 'select * from s' ββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ.. ββββββ¬βββββ¬βββββββ¬ββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββ¬ββββββββββββ.. ββsidβnameβstatusβcityβββββββ¬βββ¬βββ¬βββ¬βββββββββββ¬ββββββ¬ββββββ¬ββββββ¬βββββββ20 10 30 20 .. ββββββ΄βββββ΄βββββββ΄ββββββββs1βs2βs3βs4βs5βββsmithβjonesβblakeβclarkβadamsββ .. β ββββββ΄βββ΄βββ΄βββ΄βββββββββββ΄ββββββ΄ββββββ΄ββββββ΄βββββββ .. β βββββββββββββββββββ΄ββββββββββββββββββββββββββββββββ΄ββββββββββββ.. ββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ..
For interactive use, the function sqlreads is a cover for sqlread that returns a formatted display:
sqlreads__db 'select * from s' βββββ¬ββββββ¬βββββββ¬βββββββ βsidβname βstatusβcity β βββββΌββββββΌβββββββΌβββββββ€ βs1 βsmithβ20 βlondonβ βs2 βjonesβ10 βparis β βs3 βblakeβ30 βparis β βs4 βclarkβ20 βlondonβ βs5 βadamsβ30 βathensβ βββββ΄ββββββ΄βββββββ΄βββββββ
For convenience, the select keyword and * column specifier can be omitted:
sqlreads__db 's' βββββ¬ββββββ¬βββββββ¬βββββββ βsidβname βstatusβcity β βββββΌββββββΌβββββββΌβββββββ€ βs1 βsmithβ20 βlondonβ βs2 βjonesβ10 βparis β βs3 βblakeβ30 βparis β βs4 βclarkβ20 βlondonβ βs5 βadamsβ30 βathensβ βββββ΄ββββββ΄βββββββ΄βββββββ
Otherwise, the argument is a standard SQL statement (as supported by sqlite):
sqlreads__db 's where status=30 order by city' βββββ¬ββββββ¬βββββββ¬βββββββ βsidβname βstatusβcity β βββββΌββββββΌβββββββΌβββββββ€ βs5 βadamsβ30 βathensβ βs3 βblakeβ30 βparis β βββββ΄ββββββ΄βββββββ΄βββββββ
sqlreads__db 'sid,sum(qty),max(qty) from sp group by sid' βββββ¬βββββββββ¬βββββββββ βsidβsum(qty)βmax(qty)β βββββΌβββββββββΌβββββββββ€ βs1 β1200 β400 β βs2 β 700 β400 β βs3 β 200 β200 β βs4 β1000 β400 β βββββ΄βββββββββ΄βββββββββ
Writing Data
The sqlinsert function takes an argument of a table, followed by a pair: column_names, column_values (the same format as the result of sqlread). For example, using a copy of the sandp database:
db=: sqlcopy_psqlite_ '~addons/data/sqlite/db/sandp.db';'~temp/sandp.db' cls=: ;:'sid name status city' dat=: ('s6';'s7');('brown';'eaton');40 10;<'rome';'madrid' sqlinsert__db 's';cls;<dat 5 sqltail__db 's' NB. last 5 records βββββ¬ββββββ¬βββββββ¬βββββββ βsidβname βstatusβcity β βββββΌββββββΌβββββββΌβββββββ€ βs3 βblakeβ30 βparis β βs4 βclarkβ20 βlondonβ βs5 βadamsβ30 βathensβ βs6 βbrownβ40 βrome β βs7 βeatonβ10 βmadridβ βββββ΄ββββββ΄βββββββ΄βββββββ
The sqlupdate function (v1.0.23 and later) updates records on a where statement:
sqlupdate__db 'p';'weight=12';('name';'city');<'hammer';'vienna' sqlhead__db 'p where weight=12' βββββ¬βββββββ¬ββββββ¬βββββββ¬βββββββ βpidβname βcolorβweightβcity β βββββΌβββββββΌββββββΌβββββββΌβββββββ€ βp1 βhammerβred β12 βviennaβ βp5 βhammerβblue β12 βviennaβ βββββ΄βββββββ΄ββββββ΄βββββββ΄βββββββ
Example for update only one field:
sqlupdate__db 'p';'weight=12';(,<'name');<,<'hammer'
The sqlupsert function is a variant that will update existing records on given keys, and insert any remaining records:
dat=: ('s5';'s8');('adams';'scott');50 60;<'lisbon';'berlin' sqlupsert__db 's';'sid';cls;<dat 5 sqltail__db 's' βββββ¬ββββββ¬βββββββ¬βββββββ βsidβname βstatusβcity β βββββΌββββββΌβββββββΌβββββββ€ βs4 βclarkβ20 βlondonβ βs5 βadamsβ50 βlisbonβ βs6 βbrownβ40 βrome β βs7 βeatonβ10 βmadridβ βs8 βscottβ60 βberlinβ βββββ΄ββββββ΄βββββββ΄βββββββ