sqlite
  1. sqlite-import

SQLite Import/Export

SQLite provides several tools for importing and exporting data to and from SQLite databases. These tools can be used to move data between different SQLite databases or to import data from other data sources.

Syntax

The syntax for importing and exporting data in SQLite depends on the tool used. The most commonly used tools are:

  • The sqlite3 command-line interface
  • The .import command
  • The .output command
  • The .dump command

Example

Suppose we have a CSV file with data on employees, and we want to import that data into an SQLite database. We can use the sqlite3 command-line interface with the .import command to import the data.

sqlite3 test.db
sqlite> .mode csv
sqlite> .import employees.csv employees

In the example above, we first start the sqlite3 command-line interface and create a new SQLite database called test.db. We then set the output mode to CSV using the .mode command. Finally, we use the .import command to import the data from the employees.csv file into the SQLite database, with the data being stored in a table called employees.

Output

We can verify that the data has been imported into the database by using the SELECT statement.

sqlite> SELECT * FROM employees;

The output would show the data in the table:

id          name        email               phone
----------  ----------  -----------------  ---------------
1           John Doe    john.doe@example.com  555-555-1212
2           Jane Smith  jane.smith@example.com 555-555-1213
3           Bob Johnson bob.johnson@example.com 555-555-1214

Explanation

In the example above, we use the sqlite3 command-line interface to import data from the employees.csv file into a new SQLite database. We first set the output mode to CSV using the .mode command and then use the .import command to import the data from the CSV file. The command tells SQLite to import the data from the file and store it in a table called employees.

We then use the SELECT statement to retrieve the data from the employees table and display it on the console.

Use

Importing and exporting data in SQLite is useful for moving data between different databases or for importing data from other data sources. This is particularly useful when working with large amounts of data or when data needs to be merged from different sources.

Important Points

  • When importing data into SQLite, the data must be in a compatible format such as CSV or SQLite.
  • The output mode (-csv, -json, etc.) must be set to the same format as the data being imported.
  • The .output command can be used to set the output file for the results of a query or to redirect the output to a file.
  • The .dump command can be used to create a backup of the database in a text format.

Summary

In this tutorial, we learned about importing and exporting data in SQLite using the sqlite3 command-line interface and various commands such as .import, .output, and .dump. We saw an example of importing data from a CSV file and how to use the SELECT statement to verify that the data was imported successfully. Importing and exporting data is useful when moving data between different SQLite databases or when importing data from other data sources.

Published on: