SQLite3 : Import et export d une table



Export en CSV

To export data from the SQLite database to a CSV file, you use these steps:

The following commands select data from the customers table and export it to the data.csv file.

>sqlite3 c:/sqlite/chinook.db
sqlite> .headers on
sqlite> .mode csv
sqlite> .output data.csv
sqlite> SELECT * FROM customers;
sqlite> .quit

In the first scenario, you want to import data from CSV file into a table that does not exist in the SQLite database.

  1. First, the sqlite3 tool creates the table. The sqlite3 tool uses the first row of the CSV file as the names of the columns of the table.
  2. Second, the sqlite3 tool import data from the second row of the CSV file into the table.

To import the c:\sqlite\city.csv file into the cities table:

sqlite> .mode csv
sqlite>.import c:/sqlite/data.csv table_name

In the second scenario, the table is already available in the database and you just need to import the data.

If the table already exists, the sqlite3 tool uses all the rows, including the first row, in the CSV file as the actual data to import. Therefore, you should delete the first row of the CSV file.

The following commands import the city_without_header.csv file into the cities table.

sqlite> .mode csv
sqlite> .import c:/sqlite/city_no_header.csv cities

Export et import en mode INSERT


PubliƩ le : 09-02-2023 - 20:58