$ sqlite3 ~/Documents/Work/CRQ/CRQ.db SQLite version 3.8.10.2 2015-05-20 18:17:19 Enter ".help" for usage hints. sqlite> .schema Tasks CREATE TABLE "Tasks" ( `CRQ` TEXT NOT NULL, `Task` TEXT NOT NULL, `URL` TEXT, `Received` INTEGER, `Reviewed` INTEGER, `Approved` INTEGER, `Notes` TEXT, `Summary` TEXT ); sqlite> .exit $
I can open the database and manually type the command to count the records with an approval date on or after a date I specify using an SQL command like the one below:
$ sqlite3 ~/Documents/Work/CRQ/CRQ.db SQLite version 3.8.10.2 2015-05-20 18:17:19 Enter ".help" for usage hints. sqlite> SELECT COUNT(*) FROM Tasks WHERE Approved >= '2017-02-01'; 11 sqlite>
If I only want the number approved on a particular date, I can use
Approved =
rather than Approved >=
.
sqlite> SELECT COUNT(*) FROM Tasks WHERE Approved = '2017-02-01'; 9 sqlite>
To save a little time each week, I'd like to be able to save the command in
a file and have sqlite3 execute the command from the file, so I don't need to
retype it. You can store SQL commands in a text file and have sqlite3 read the
commands stored in the file using
redirection. E.g., I could put the command SELECT COUNT(*) FROM Tasks
WHERE Approved >= '2017-02-01';
in a file named
WeeklyApprovalsCount.txt
and have sqlite3 run the command by
entering the command below in a
Terminal window.
$ sqlite3 ~/Documents/Work/CRQ/CRQ.db < ~/Documents/Work/CRQ/WeeklyApprovalsCount.txt 11 $
If I needed to run additional commands, I could add them as additional lines in the file.
But I don't want to have to edit the file each week to change the date. I
can use CURRENT_DATE
for today's date rather than specifying the
date each week.
sqlite> SELECT COUNT(*) FROM Tasks WHERE Approved = CURRENT_DATE; 1 slquite>
But I need to subtract 7 days from the current date in order to look for records in the table approved within the last week. To do that, I can use the DATE function as shown below2.
sqlite> SELECT COUNT(*) FROM Tasks WHERE Approved >= date(CURRENT_DATE,'-7 day'); 11 sqlite>
So I could put SELECT COUNT(*) FROM Tasks WHERE Approved >=
DATE(CURRENT_DATE,'-7 day');
in the text file
WeeklyApprovalsCount.txt
, instead, and not have to change the date
each week. I could also use 'now'
, instead of
CURRENT_DATE
.
sqlite> SELECT COUNT(*) FROM Tasks WHERE Approved >= date('now','-7 day'); 11 sqlite>
References: