blaze / odo

Data Migration for the Blaze Project
http://odo.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
1k stars 139 forks source link

Importing CSV to sqlite3 fails due to invalid command-line syntax #482

Open giladarnold opened 7 years ago

giladarnold commented 7 years ago

When importing a CSV odo invokes sqlite3 with command-line arguments: sqlite3 -nullvalue '' -separator , -cmd '.import "/path/to/csv/file" tablename' /path/to/db/file

However, current sqlite3 (version 3.6.20) does not support a -cmd option. Instead, the invocation should look like this: sqlite3 -nullvalue '' -separator , /path/to/db/file '.import "/path/to/csv/file" tablename'

I patched odo/backends/sql_csv.py:compile_from_csv_sqlite() accordingly and it works like a charm. Happy to submit the patch if that'll speed things up. Please advise, thanks!

kwmsmith commented 7 years ago

@giladarnold yes, please submit the patch, thanks.

giladarnold commented 7 years ago
From 064c7f131247bc904fd7144f051f13a76b5caea4 Mon Sep 17 00:00:00 2001
From: Gilad Arnold <gilad.arnold@gmail.com>
Date: Fri, 7 Oct 2016 15:32:22 -0700
Subject: [PATCH] Fix sqlite CSV import command.

Current sqlite3 binaries do not take a -cmd option. They use positional
arguments instead. This fixes the problem for CSV import.
---
 odo/backends/sql_csv.py |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/odo/backends/sql_csv.py b/odo/backends/sql_csv.py
index d2277e5..307da65 100644
--- a/odo/backends/sql_csv.py
+++ b/odo/backends/sql_csv.py
@@ -85,11 +85,11 @@ def compile_from_csv_sqlite(element, compiler, **kwargs):
     cmd = ['sqlite3',
            '-nullvalue', repr(element.na_value),
            '-separator', element.delimiter,
-           '-cmd', '.import "%s" %s' % (
+           element.bind.url.database,
+           '.import "%s" %s' % (
                # FIXME: format_table(t) is correct, but sqlite will complain
                fullpath, compiler.preparer.format_table(t)
-           ),
-           element.bind.url.database]
+           )]
     stderr = subprocess.check_output(
         cmd,
         stderr=subprocess.STDOUT,
-- 
1.7.1
giladarnold commented 7 years ago

@kwmsmith this looks okay?

giladarnold commented 7 years ago

Created pull request: https://github.com/blaze/odo/pull/488

rand-Mal-Function commented 7 years ago

I Just got it working by replacing the filename with pandas.read_csv(filename) example : db = odo(pandas.read_csv(filename) , "sqlite:///mydb.db::tablename" , dshape=dshape)