SQLite Export Data from Table to CSV File

Here we will learn how to export data from sqlite database tables to CSV or excel external files using .output command in sqlite with examples.

SQLite Export Data to CSV File

In SQLite, by using “.output” command we can export data from database tables to CSV or excel external files based on our requirement.

Syntax of SQLite Export Command

Following is the syntax of “.output” command to export data from the database to CSV or excel file.

 

.output (filename)

SQLite Export Data to CSV File Example

We will export “emp_master” table data to Employee.csv file for that write the query like as shown below.  Let’s look at the example of exporting data of emp_master table to Employee.csv file. This file does not exist. So it will first create and export data into it.

 

sqlite> .header on

sqlite> .mode csv

sqlite> .output Employee.csv

sqlite> SELECT * FROM emp_master;

sqlite> .quit

If you observe above example to export data from SQLite database to CSV or Excel file we followed few steps those are

 

  1. To insert table column names in CSV or Excel file we used .header on command.
  2. To return the data in CSV format we used .mode CSV.
  3. To send data to CSV file we used .output command and SELECT statement to export data from the required table.

Once we execute the above statements Employee.csv file will create in the folder where our SQLite3.exe file exists with emp_master table data like shown following.

 

SQLite Export Data from Table to CSV File

 

When we open the Employee.csv file that will contain all the records of emp_master table like as shown below.

 

emp_id,first_name,last_name,salary,dept_id

1,Honey,Patel,10100,1

2,Shweta,Jariwala,19300,2

3,Vinay,Jariwala,35100,3

4,Jagruti,Viras,9500,2

5,Shweta,Rana,12000,3

6,Sonal,Menpara,13000,1

7,Yamini,Patel,10000,2

8,Khyati,Shah,50000,3

9,Shwets,Jariwala,19400,2

This is how we can export data from SQLite database to CSV or EXCEL files based on our requirements.