From Newsgroup: comp.lang.awk
On 3/1/24 9:29 AM, Mr. Man-wai Chang wrote:
Can Awk directly interact with some RDBMS engine (that is, within 'BEGIN{}')?
I haven't done a Google Search yet. Is looking for a direct answer. :)
I *think* most SQL type DBs have CLI interaction capabilities.
A SQLite3 example; 'csv' used for DB output mode since it's easy to run split() on and several awks now have native CSV support:
--
$ sqlite3 -header -column site.db 'select * from site'
Station SSID CHAN
---------- ---------- ----------
1 CIA 9
2 FBI 11
3 NSA 1
4 DOD 10
$ cat site.awk
BEGIN {
DBfile = "./site.db"
CMD = "sqlite3 -csv " DBfile " \"" ARGV[1] "\""
while (CMD | getline == 1) arr[++cnt] = $0
close(CMD)
for (i=1; i<=cnt; i++) printf "arr[%d] = %s\n", i, arr[i]
print ""
}
$ nawk -f site.awk 'select * from site'
arr[1] = 1,CIA,9
arr[2] = 2,FBI,11
arr[3] = 3,NSA,1
arr[4] = 4,DOD,10
$ nawk -f site.awk 'select SSID from site'
arr[1] = CIA
arr[2] = FBI
arr[3] = NSA
arr[4] = DOD
--
This is just read-only of course; would need to use gawk for fancier things.
--- Synchronet 3.20a-Linux NewsLink 1.114