How to insert CSV data into an SQLite table via a shell pipe?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite
1












I have written a program that outputs the result to the standard output in strict pure CSV form (every line represents a single record and contain the same set of comma-separated fields, fields only contain lowercase English letters, numbers and dots, no spaces, no quotes and no symbols that might need to be escaped/encoded).



How do I redirect this output into an SQLite table that it fits into perfectly?



It would be great if I could control whether I want constraint-breaking (e.g. having the same primary/secondary key as the records already in the table) replace existing records or be discarded silently.



Of course I might build direct SQLite database output support in the program itself but I would prefer going the unix-way if possible.







share|improve this question






















  • Has outputted csv a header columns as the 1st line?
    – RomanPerekhrest
    Feb 16 at 9:04











  • @RomanPerekhrest Not really. But of course covering both cases will make this Q&A more interesting.
    – Ivan
    Feb 16 at 10:14















up vote
1
down vote

favorite
1












I have written a program that outputs the result to the standard output in strict pure CSV form (every line represents a single record and contain the same set of comma-separated fields, fields only contain lowercase English letters, numbers and dots, no spaces, no quotes and no symbols that might need to be escaped/encoded).



How do I redirect this output into an SQLite table that it fits into perfectly?



It would be great if I could control whether I want constraint-breaking (e.g. having the same primary/secondary key as the records already in the table) replace existing records or be discarded silently.



Of course I might build direct SQLite database output support in the program itself but I would prefer going the unix-way if possible.







share|improve this question






















  • Has outputted csv a header columns as the 1st line?
    – RomanPerekhrest
    Feb 16 at 9:04











  • @RomanPerekhrest Not really. But of course covering both cases will make this Q&A more interesting.
    – Ivan
    Feb 16 at 10:14













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I have written a program that outputs the result to the standard output in strict pure CSV form (every line represents a single record and contain the same set of comma-separated fields, fields only contain lowercase English letters, numbers and dots, no spaces, no quotes and no symbols that might need to be escaped/encoded).



How do I redirect this output into an SQLite table that it fits into perfectly?



It would be great if I could control whether I want constraint-breaking (e.g. having the same primary/secondary key as the records already in the table) replace existing records or be discarded silently.



Of course I might build direct SQLite database output support in the program itself but I would prefer going the unix-way if possible.







share|improve this question














I have written a program that outputs the result to the standard output in strict pure CSV form (every line represents a single record and contain the same set of comma-separated fields, fields only contain lowercase English letters, numbers and dots, no spaces, no quotes and no symbols that might need to be escaped/encoded).



How do I redirect this output into an SQLite table that it fits into perfectly?



It would be great if I could control whether I want constraint-breaking (e.g. having the same primary/secondary key as the records already in the table) replace existing records or be discarded silently.



Of course I might build direct SQLite database output support in the program itself but I would prefer going the unix-way if possible.









share|improve this question













share|improve this question




share|improve this question








edited Feb 16 at 10:14

























asked Feb 16 at 8:36









Ivan

5,484176297




5,484176297











  • Has outputted csv a header columns as the 1st line?
    – RomanPerekhrest
    Feb 16 at 9:04











  • @RomanPerekhrest Not really. But of course covering both cases will make this Q&A more interesting.
    – Ivan
    Feb 16 at 10:14

















  • Has outputted csv a header columns as the 1st line?
    – RomanPerekhrest
    Feb 16 at 9:04











  • @RomanPerekhrest Not really. But of course covering both cases will make this Q&A more interesting.
    – Ivan
    Feb 16 at 10:14
















Has outputted csv a header columns as the 1st line?
– RomanPerekhrest
Feb 16 at 9:04





Has outputted csv a header columns as the 1st line?
– RomanPerekhrest
Feb 16 at 9:04













@RomanPerekhrest Not really. But of course covering both cases will make this Q&A more interesting.
– Ivan
Feb 16 at 10:14





@RomanPerekhrest Not really. But of course covering both cases will make this Q&A more interesting.
– Ivan
Feb 16 at 10:14











1 Answer
1






active

oldest

votes

















up vote
2
down vote













Two approaches:



Sample 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) With csvkit (a suite of command-line tools for converting to and working with CSV)



Import into sqlite3 database:



csvsql --db sqlite:///test_db --tables test_tbl --insert test.csv


If no input csv file was specified it'll accept csv data from stdin:



... | csvsql --db sqlite:///test_db --tables test_tbl --insert


To extract data from sqlite database:



sql2csv --db sqlite:///test_db --query 'select * from test_tbl limit 3'


The output:



GroupName,Groupcode,GroupOwner,GroupCategoryID 
System Administrators,sysadmin,13456,100
Independence High Teachers,HS Teachers,,101
John Glenn Middle Teachers,MS Teachers,13458,102



2) With sqlite3 command-line tool (allows the user to manually enter and execute SQL statements against an SQLite database)




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>





share|improve this answer




















  • csvkit uses agate's from_csv, which, for reasons which are unclear to me, reads in the entire contents of stdin github.com/wireservice/agate/blob/…
    – Alex Coventry
    Aug 21 at 7:07











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f424555%2fhow-to-insert-csv-data-into-an-sqlite-table-via-a-shell-pipe%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













Two approaches:



Sample 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) With csvkit (a suite of command-line tools for converting to and working with CSV)



Import into sqlite3 database:



csvsql --db sqlite:///test_db --tables test_tbl --insert test.csv


If no input csv file was specified it'll accept csv data from stdin:



... | csvsql --db sqlite:///test_db --tables test_tbl --insert


To extract data from sqlite database:



sql2csv --db sqlite:///test_db --query 'select * from test_tbl limit 3'


The output:



GroupName,Groupcode,GroupOwner,GroupCategoryID 
System Administrators,sysadmin,13456,100
Independence High Teachers,HS Teachers,,101
John Glenn Middle Teachers,MS Teachers,13458,102



2) With sqlite3 command-line tool (allows the user to manually enter and execute SQL statements against an SQLite database)




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>





share|improve this answer




















  • csvkit uses agate's from_csv, which, for reasons which are unclear to me, reads in the entire contents of stdin github.com/wireservice/agate/blob/…
    – Alex Coventry
    Aug 21 at 7:07















up vote
2
down vote













Two approaches:



Sample 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) With csvkit (a suite of command-line tools for converting to and working with CSV)



Import into sqlite3 database:



csvsql --db sqlite:///test_db --tables test_tbl --insert test.csv


If no input csv file was specified it'll accept csv data from stdin:



... | csvsql --db sqlite:///test_db --tables test_tbl --insert


To extract data from sqlite database:



sql2csv --db sqlite:///test_db --query 'select * from test_tbl limit 3'


The output:



GroupName,Groupcode,GroupOwner,GroupCategoryID 
System Administrators,sysadmin,13456,100
Independence High Teachers,HS Teachers,,101
John Glenn Middle Teachers,MS Teachers,13458,102



2) With sqlite3 command-line tool (allows the user to manually enter and execute SQL statements against an SQLite database)




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>





share|improve this answer




















  • csvkit uses agate's from_csv, which, for reasons which are unclear to me, reads in the entire contents of stdin github.com/wireservice/agate/blob/…
    – Alex Coventry
    Aug 21 at 7:07













up vote
2
down vote










up vote
2
down vote









Two approaches:



Sample 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) With csvkit (a suite of command-line tools for converting to and working with CSV)



Import into sqlite3 database:



csvsql --db sqlite:///test_db --tables test_tbl --insert test.csv


If no input csv file was specified it'll accept csv data from stdin:



... | csvsql --db sqlite:///test_db --tables test_tbl --insert


To extract data from sqlite database:



sql2csv --db sqlite:///test_db --query 'select * from test_tbl limit 3'


The output:



GroupName,Groupcode,GroupOwner,GroupCategoryID 
System Administrators,sysadmin,13456,100
Independence High Teachers,HS Teachers,,101
John Glenn Middle Teachers,MS Teachers,13458,102



2) With sqlite3 command-line tool (allows the user to manually enter and execute SQL statements against an SQLite database)




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>





share|improve this answer












Two approaches:



Sample 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) With csvkit (a suite of command-line tools for converting to and working with CSV)



Import into sqlite3 database:



csvsql --db sqlite:///test_db --tables test_tbl --insert test.csv


If no input csv file was specified it'll accept csv data from stdin:



... | csvsql --db sqlite:///test_db --tables test_tbl --insert


To extract data from sqlite database:



sql2csv --db sqlite:///test_db --query 'select * from test_tbl limit 3'


The output:



GroupName,Groupcode,GroupOwner,GroupCategoryID 
System Administrators,sysadmin,13456,100
Independence High Teachers,HS Teachers,,101
John Glenn Middle Teachers,MS Teachers,13458,102



2) With sqlite3 command-line tool (allows the user to manually enter and execute SQL statements against an SQLite database)




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>






share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 16 at 9:47









RomanPerekhrest

22.4k12144




22.4k12144











  • csvkit uses agate's from_csv, which, for reasons which are unclear to me, reads in the entire contents of stdin github.com/wireservice/agate/blob/…
    – Alex Coventry
    Aug 21 at 7:07

















  • csvkit uses agate's from_csv, which, for reasons which are unclear to me, reads in the entire contents of stdin github.com/wireservice/agate/blob/…
    – Alex Coventry
    Aug 21 at 7:07
















csvkit uses agate's from_csv, which, for reasons which are unclear to me, reads in the entire contents of stdin github.com/wireservice/agate/blob/…
– Alex Coventry
Aug 21 at 7:07





csvkit uses agate's from_csv, which, for reasons which are unclear to me, reads in the entire contents of stdin github.com/wireservice/agate/blob/…
– Alex Coventry
Aug 21 at 7:07













 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f424555%2fhow-to-insert-csv-data-into-an-sqlite-table-via-a-shell-pipe%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?