Skip to content

Instantly share code, notes, and snippets.

@hideshi
Created December 28, 2013 11:43
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hideshi/8158582 to your computer and use it in GitHub Desktop.
Save hideshi/8158582 to your computer and use it in GitHub Desktop.
How to use SQLite3 in Awk.
#!/usr/bin/awk -f
BEGIN {
db = "test.db"
command = "sqlite3 -noheader -separator \" \" " db " \" %s \""
create_table = "create table employee (id, name, age)"
insert1 = "insert into employee values (1, 'taro', 20)"
insert2 = "insert into employee values (2, 'hanako', 18)"
insert3 = "insert into employee values (3, 'ichiro', 26)"
select = "select * from employee order by age"
drop_table = "drop table employee"
system(sprintf(command, create_table))
system(sprintf(command, insert1))
system(sprintf(command, insert2))
system(sprintf(command, insert3))
while((sprintf(command, select) | getline) > 0) {
print $2, $3
}
system(sprintf(command, drop_table))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment