8000 docs for writing data to disk · pythonthings/q@9e4111b · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e4111b

Browse files
committed
docs for writing data to disk
1 parent eb8c455 commit 9e4111b

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

examples/EXAMPLES.markdown

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Tutorial steps:
5050
* The timestamp is assumed to be a unix epoch timestamp, but in ms, and DATETIME accepts seconds, so we need to divide by 1000
5151
* The full-minute rounding is done by dividing by 60000 (ms), rounding and then multiplying by the same amount. Rounding to an hour, for example, would be the same except for having 3600000 instead of 60000.
5252
* We use DATETIME's capability in order to output the time in localtime format. In that case, it's converted to New York time (hence the -5 hours)
53-
* The filename is actually all files matching "datafile*.gz" - Multiple files can be read, and since they have a .gz extension, they are decompressed on the fly.
53+
* The filename is actually all files matching `datafile*.gz` - Multiple files can be read, and since they have a .gz extension, they are decompressed on the fly.
5454
* **NOTE:** For non-SQL people, the date manipulation may seem odd at first, but this is standard SQL processing for timestamps and it's easy to get used to.
5555

5656
## JOIN example
@@ -70,6 +70,35 @@ ppp dip.2@otherdomain.com
7070

7171
You can see that the ppp filename appears twice, each time matched to one of the emails of the group `dip` to which it belongs. Take a look at the files [`exampledatafile`](exampledatafile) and [`group-emails-example`](group-emails-example) for the data.
7272

73+
## Writing the data into an sqlite3 database
74+
q now supports writing its data into a disk base sqlite3 database file. In order to write the data to a database disk use the `-S` parameter (`--save-db-to-disk`) with a filename as a parameter. Note that you still need to provide a query as a parameter, even though it will not be executed. The tool will provide the proper sqlite3 query to run after writing the data to the database, allowing you to copy-paste it into the sqlite3 command line. If you don't care about running any query, just use "select 1" as the query.
75+
76+
Here's an example that will write the output into `some.db` for further processing. Note that we've added the `-c 1` parameter to prevent q warning us about having only one column.
77+
```
78+
$ seq 1 100 | ./q "select count(*) from -" -S some.db -c 1
79+
Going to save data into a disk database: some.db
80+
Data has been loaded in 0.002 seconds
81+
Saving data to db file some.db
82+
Data has been saved into some.db . Saving has taken 0.018 seconds
83+
Query to run on the database: select count(*) from `-`;
84+
85+
$ sqlite3 some.db
86+
SQLite version 3.19.3 2017-06-27 16:48:08
87+
Enter ".help" for usage hints.
88+
sqlite> .tables
89+
-
90+
sqlite> .schema
91+
CREATE TABLE IF NOT EXISTS "-" ("c1" INT);
92+
sqlite> select count(*) from `-`;
93+
100
94+
sqlite>
95+
```
96+
97+
Note that table names are explictly set to the filenames in the original query (e.g. filenames), which means that in many cases you'd need to escape the table names in sqlite3 with backticks. For example, the name of the table above is `-`, and in order to use it in an sqlite3 query, it is backticked, otherwise it won't conform to a proper table name. I've decided to emphasize consistency and simplicity in this case, instead of trying to provide some normalization/sanitation of filenames, since I believe that doing it would cause much confusion and will be less effective. Any ideas and comments are this are most welcome obviously.
98+
99+
### Choosing the method of writing the sqlite3 database
100+
There's another parameter that controls the method of writing to the sqlite3 database - `--save-db-to-disk-method`. The value can either be `standard` or `fast`. The fast method requires changes in the packaging of q, since it's dependent on another python module (https://github.com/husio/python-sqlite3-backup by @husio - Thanks!). However, there are some complications with seamlessly packaging it without possibly causing some backward compatibility issues (see PR #159 for some details), so it's not the standard method as of yet. If you're an advanced user, and in need for the faster method due to very large files etc., you'd need to manually install this python package for the fast method to work - Run `pip install sqlitebck` on your python installation. Obviously, I'm considering this as a bug that I need to fix.
101+
73102
## Installation
74103
Installation instructions can be found [here](../doc/INSTALL.markdown)
75104

0 commit comments

Comments
 (0)
0