MJN All Blog Cheatsheets Elasticsearch GCP JS LinuxBash Misc Notes Other ShortcutKeys / - Search

Home / LinuxBash / Script sqlite-load-csv - Load a csv file into a sqlite3 database


#!/bin/bash
help_text="
NAME
  sqlite-load-csv - Load a csv file into a sqlite3 database.

USAGE
  sqlite-load-csv [options] <csv_filename> [<database_name>]

OPTIONS
  -h|--help
    Show help text.

  -t|--table <table name>
    Specify the name of the table created.

DESCRIPTION
  Loads a csv file into a table with the same name as the file (or passed in as an option
  t|table) into a database the name of which is passed as a parameter or if no database name
  is specified, a database with the same name as the csv file.

  The column names are read from line 1 of the csv file.

  NOTE: Strips spaces from either side a comma otherwise these are added to
  to both the column names and the field values.

AUTHOR
  mjnurse.dev - 2020
"
help_line="Load a csv file into a sqlite3 database"
web_desc_line="Load a csv file into a sqlite3 database"

try="Try ${0##*/} -h for more information"
tmp="${help_text##*USAGE}"
usage="$(echo Usage: ${tmp%%OPTIONS*})"

if [[ "$1" == "" ]]; then
  echo "${usage}"
  echo "${try}"
  exit 1
fi

table_name=""

while [[ "$1" != "" ]]; do
   case $1 in
      -h|--help)
         echo "$help_text"
         exit
         ;;
      -t|--table)
         shift
         table_name="$1"
         ;;
      ?*)
         break
         ;;
   esac
   shift
done

csv_filename="$1"
if [[ "$table_name" == "" ]]; then
  table_name="${csv_filename/.csv/}"
fi
db_name="${2/.db/}"

if [[ "$db_name" == "" ]]; then
  db_name="${csv_filename/.csv/}"
fi 
db_name="${db_name}.db"

echo loading: $csv_filename into table: $table_name in database: $db_name 

cat $csv_filename | sed "s/ *, */,/g" > $0.tmpcsv

echo 'drop table '$table_name';' | sqlite3 $db_name > /dev/null 2>&1

echo '
.mode csv
.import '$0.tmpcsv' '$table_name'
' > $0.tmp

cat $0.tmp | sqlite3 $db_name 

rm -f $0.tmpcsv $0.tmp



This page was generated by GitHub Pages. Page last modified: 23/02/07 08:59