Advertisement
metalx1000

Import and Export CSV to/from sqlite database

Oct 15th, 2022 (edited)
1,376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.67 KB | Source Code | 0 0
  1. #create csv file
  2. wget "https://randomuser.me/api/?results=100&format=csv" -qO /tmp/peoplecsv/people.csv
  3.  
  4. #create database
  5. sqlite3 people.sqlite
  6.  
  7. #set mode and import CSV - note that first line will be field names
  8. .mode csv
  9. .import people.csv people
  10.  
  11. #check things where imported correctly
  12. PRAGMA table_info(people);
  13. select * from people;
  14.  
  15. #######Export to CSV######
  16. #export single table
  17. sqlite3 -header -csv people.sqlite "select * from people;" > output.csv
  18.  
  19. #if you have more than one table, you can select which table you want with fzf
  20. table="$(sqlite3 people.sqlite ".tables"|tr " " "\n"|sort -u|fzf)"
  21. sqlite3 -header -csv people.sqlite "select * from $table;"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement