Execute SQL queries directly on one or more CSV files #276
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In #259, with the new tool
sql2csv, it was already suggested that you could execute SQL queries directly on CSV files using a hacky bash script. Now, with these small changes tocsvsql, you can truly execute SQL queries on one or more CSV files.This functionality is similar to TextQL and Q. However, thanks to the solid codebase of
csvkit,csvsqlhandles both multiple tables and proper escaping of output. The following example, which joins two data sets that can be found in the examples directory, illustrates the changes:Note that you can now mix standard input and filenames. It is no longer necessary to specify
--tablewhen standard input is provided. Tables based on standard input are named "stdin" by default. Table names can still be overridden by specifying a (comma delimited) string to--tables. When--queryis specified and--dbis not,sqlite:///:memory:is used as the connection string and--insertis set to True. Multiple SQL queries can be specified by using ";" as a delimiter. Only the output of the last query is outputted as CSV. This allows you to execute SQL queries in memory.No functionality has been removed. Appropriate tests have been added. I'll write the corresponding documentation once these changes have been accepted.