question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support "create table if not exists"

See original GitHub issue

When 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:closed
  • Created 6 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
jpmckinneycommented, Mar 26, 2017

--create-if-not-exists is clear and good. I think we’d just need to add a keyword argument in agate-sql and use checkfirst= in the sql_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 😃

0reactions
jestcommented, Apr 2, 2017

Sure, #818.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PostgreSQL: Create table if not exists AS - Stack Overflow
CREATE TABLE table_name AS (....query...) it works just fine. But then if I add 'if not exists' and run: CREATE ...
Read more >
How to create table in MySQL using “if not exists” technique
CREATE TABLE if not exists Grocery_bill (Employee_Id INT, Employee_name VARCHAR(50));. The command runs successfully but we discussed above that the two tables ...
Read more >
CREATE TABLE IF NOT EXISTS equivalent in SQL Server
Microsoft SQL Server lacks the function of create table if not exists , meaning table creation queries will fail if the table already...
Read more >
17.5.1.6 Replication of CREATE ... IF NOT EXISTS Statements
Every CREATE DATABASE IF NOT EXISTS statement is replicated, whether or not the database already exists on the source. Similarly, every CREATE TABLE...
Read more >
create table if not exists? — oracle-tech
You can do it using PL/SQL Block. Check for table name in user_tables and if table does not exists then create it using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found