Created
December 28, 2013 11:43
-
-
Save hideshi/8158582 to your computer and use it in GitHub Desktop.
How to use SQLite3 in Awk.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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