test.csv
looks like this:
"Code","Member","Position","ModDate"
"AAN","Smith, John",,1/21/2020 0:00:00
"AAN","Doe, Ron",,1/21/2020 0:00:00
"AAN","Doe, Jane","C",1/21/2020 0:00:00
Note that 2 values in the Position
column are missing. They're blank. There are no quotes, no ''
or ""
, just empty space between 2 commas: ,,
.
Is it possible to import such values as NULL
into SQLite using the sqlite3
CLI? So far, it'll only import them as empty strings: ''
. (Yes, I could use Python or run UPDATE ... SET
(see later), but I'm wondering if a simple CLI command will suffice.)
Put another way, I want to run this query to get rows with missing positions:
SELECT *
FROM tbl
WHERE position IS NULL
;
But so far I can only achieve that result with this query:
SELECT *
FROM tbl
WHERE position = ''
;
First I tried the default:
sqlite3 -csv test.db ".import test.csv tbl"
As I said, this imports null values as empty strings.
sqlite3
's -help
text mentions a -nullvalue
option:
-nullvalue TEXT set text string for NULL values. Default ''
I assume this is for importing. I try this:
sqlite3 -csv -nullvalue ",," test.db ".import test.csv tbl"
No luck. And I don't know what else to try. I can't find any further explanation of the -nullvalue
option in SQLite's docs or any examples with Google.
Perhaps I could just update these values after importing, as answered here:
sqlite3 -csv test.db ".import test.csv tbl"
sqlite3 test.db
sqlite> UPDATE tbl SET position = NULL WHERE position = '';
However, this isn't entirely satisfactory. First, I have multiple CSVs and multiple columns within each of them that I must update. This would require repetition. Is there a better way?