Duas abordagens:
Amostra test.csv
file:
GroupName,Groupcode,GroupOwner,GroupCategoryID
System Administrators,sysadmin,13456,100
Independence High Teachers,HS Teachers,,101
John Glenn Middle Teachers,MS Teachers,13458,102
Liberty Elementary Teachers,Elem Teachers,13559,103
1st Grade Teachers,1stgrade,,104
2nd Grade Teachers,2nsgrade,13561,105
3rd Grade Teachers,3rdgrade,13562,106
Guidance Department,guidance,,107
1 ) Com csvkit (um conjunto de ferramentas de linha de comando para converter e trabalhar com CSV)
Importe para o banco de dados sqlite3 :
csvsql --db sqlite:///test_db --tables test_tbl --insert test.csv
Se nenhum arquivo csv de entrada foi especificado, ele aceitará dados csv do stdin:
... | csvsql --db sqlite:///test_db --tables test_tbl --insert
Para extrair dados do banco de dados sqlite :
sql2csv --db sqlite:///test_db --query 'select * from test_tbl limit 3'
A saída:
GroupName,Groupcode,GroupOwner,GroupCategoryID
System Administrators,sysadmin,13456,100
Independence High Teachers,HS Teachers,,101
John Glenn Middle Teachers,MS Teachers,13458,102
2 ) Com a ferramenta de linha de comando sqlite3 (permite que o usuário para inserir manualmente e executar instruções SQL em um banco de dados SQLite )
Use the "
.import
" command to import CSV (comma separated value) data into an SQLite table. The ".import
" command takes two arguments which are the name of the disk file from which CSV data is to be read and the name of the SQLite table into which the CSV data is to be inserted.Note that it is important to set the "mode" to "csv" before running the "
.import
" command. This is necessary to prevent the command-line shell from trying to interpret the input file text as some other format.
$ sqlite3
sqlite> .mode csv
sqlite> .import test.csv test_tbl
sqlite> select GroupName,Groupcode from test_tbl limit 5;
"System Administrators",sysadmin
"Independence High Teachers","HS Teachers"
"John Glenn Middle Teachers","MS Teachers"
"Liberty Elementary Teachers","Elem Teachers"
"1st Grade Teachers",1stgrade
sqlite>