Support "create table if not exists"
See original GitHub issueWhen importing multiple CSV files into a SQL (which all share the same table schema), it would be great if csvsql used CREATE TABLE IF NOT EXISTS foo
.
Current vs. Desired Usage
Consider this shell code, which dynamically generates CSV files (say, by making requests to a JSON API, munging the data, and spitting out a CSV file):
for thing in $THINGS; do
# ...
# process $thing, generate $thing.csv
# ...
# Load $thing.csv into SQLite database
csvsql --db sqlite:///foo.db --insert $thing.csv
done
This will currently fail for every loop iteration after the first $thing
, because it uses this SQL to create the table:
CREATE TABLE ... (
...
);
Ideally, I’d be able to have a flag like --create-if-not-exists
(substitute the name of your choice here, like --create
, --create-force
, --create-silent
, etc.), which generates this SQL:
CREATE TABLE IF NOT EXISTS ... (
...
);
Implementation Considerations
It’s possible this change would have to be made in accordance with an upstream change to agate-sql
. If this is not the correct place to report an issue, let me know.
If this is a feature you’d consider, but you don’t want to do it yourself, I’d be glad to lend a hand.
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (11 by maintainers)
--create-if-not-exists
is clear and good. I think we’d just need to add a keyword argument in agate-sql and usecheckfirst=
in thesql_table.create()
line. Then add the command-line option to csvkit’s csvsql tool, add some tests, and update the changelog. If you can do that, it’d be much appreciated 😃Sure, #818.