Guest User

sqlite csv to table

a guest
Aug 25th, 2021
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.66 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. function usage {
  4.     echo "Usage: $0 <csv_file> [<table_name>]"
  5.     exit 1
  6. }
  7.  
  8. [ $# -lt 1 ] && usage
  9. csvfile="$1"
  10. [ ! -s "$csvfile" ] && echo "missing or empty file: $csvfile" && exit 1
  11.  
  12. tablename="${csvfile%%\.csv}"
  13. [ -z "$tablename" ] && echo "unable to determine tablename"
  14. if [[ $tablename =~ [-] ]] ; then
  15.     echo "tablename cannot contain hypen - converting to underscore (_)"
  16.     tablename=$(echo "$tablename"|tr "-" "_")
  17. fi
  18.  
  19. echo "csv file  : $csvfile"
  20. echo "table name: $tablename"
  21.  
  22. sqlite3 -interactive \
  23.     -cmd ".mode csv" \
  24.     -cmd ".mode columns" \
  25.     -cmd ".headers on" \
  26.     -cmd ".import $csvfile '$tablename'"
Advertisement
Add Comment
Please, Sign In to add comment