How can SQLite command ".import" read directly from standard input?
10
votes
1
answer
3322
views
How would you modify function
csv_to_sqlite
so that [sqlite3
](https://sqlite.org/cli.html) command [.import
](https://sqlite.org/cli.html#importing_csv_files) reads directly from standard input instead of from a temporary named pipe?
#!/bin/bash
function csv_to_sqlite() {
local database_file_name="$1"
local table_name="$2"
local temp_input_fifo=$(mktemp -u)
mkfifo $temp_input_fifo
sqlite3 -csv $database_file_name ".import $temp_input_fifo $table_name" &
cat > $temp_input_fifo
rm $temp_input_fifo
}
database_file_name=$1
table_name=$2
csv_to_sqlite "$database_file_name" "$table_name"
$ printf "col1,col2,col3\na,1,2.6\nb,2,5.4\n" | ./csv_to_sqlite test.sqlite test
$ sqlite3 -csv -header test.sqlite "SELECT * FROM test"
col1,col2,col3
a,1,2.6
b,2,5.4
Asked by Derek Mahar
(567 rep)
Mar 25, 2021, 09:46 PM
Last activity: Sep 4, 2024, 09:54 PM
Last activity: Sep 4, 2024, 09:54 PM