Running SQL code using the SQLite shell

  1. Open a command prompt and cd to the folder location of the SN7577.sqlite database file.

  2. run the command sqlite3 This should open the SQLite shell and present a screen similar to that below.

image

  1. By default a transient in-memory database is opened. You can change the database by use of the .open command
.open SN7577.sqlite

use example

SELECT *
FROM question1;
image
image

The output from the query is displayed on the screen. If we just wanted to look at a small selection of data this may be OK. It is however more likely that not only are the results from the query somewhat larger, but also we would prefer to save the output to a file for later use. We might also want to change the field seperator from the default “|” to a comma so that we get a standard csv file.

These problems can be resolved with further “dot” commands.

There are in fact a large number of “dot” commands and they are all explained in the official SQLite documentation here.

The commands we need are:

.mode csv
.output my.filename
  • to direct the output to a file of my choice.
  • The file will be created if needed or it will overwrite an already existing file, so exercise care.
image
image
  • The contents of the output file contains the expected output from the query in CSV format.
LS0tCnRpdGxlOiAiU1FMaXRlIGNvbW1hbmQgbGluZSIKb3V0cHV0OiBodG1sX25vdGVib29rCi0tLQoKIyMgUnVubmluZyBTUUwgY29kZSB1c2luZyB0aGUgU1FMaXRlIHNoZWxsCgoqIEJlZm9yZSB5b3UgY2FuIHJ1biB0aGUgU1FMaXRlMyBzaGVsbCBwcm9ncmFtIHlvdSBtdXN0IGhhdmUgaW5zdGFsbGVkIGl0LiBJbnN0cnVjdGlvbnMgZm9yIGRvaW5nIHRoaXMgYXJlIGluY2x1ZGVkIGluIHRoZSBzZXQgdXAgcHJvY2VkdXJlcy4KaHR0cHM6Ly9kYXRhLWxlc3NvbnMuZ2l0aHViLmlvL3NxbC1zb2NpYWxzY2kvc2V0dXAKCiogT3B0aW9uYWw6IENoZWNrIHRoYXQgeW91IGhhdmUgYWRkZWQgdGhlIGxvY2F0aW9uIG9mIHRoZSBwcm9ncmFtIHRvIHlvdXIgbG9jYWwgUEFUSCBlbnZpcm9ubWVudCB2YXJpYWJsZSBhcyB0aGlzIHdpbGwgbWFrZSBpdCBlYXNpZXIgdG8gcmVmZXIgdG8gdGhlIGRhdGFiYXNlIGZpbGUgYW5kIG90aGVyIGZpbGVz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