Sep 28, 2024
10 minute read

How to Query CSV Files with SQLite


Have you ever struggled to open large CSV files and wished for a simpler solution to run SQL queries over them without heavy database setups? That's where SQLite shines—it's lightweight, file-based, and requires zero configuration. In this guide, we’ll show you how to efficiently import and query large CSV documents, both with and without a GUI. You'll learn how to do this with the DBeaver database client for a more user-friendly, visual experience, and then directly through the SQLite CLI for a straightforward and much more performant way.

If you want to work with CSV data in SQLite, you have these options:

  1. Using a GUI like the DBeaver database client.
  2. The .import command in the SQLite database which is the most straightforward and used approach to import CSV files.
  3. The CSV Virtual Table for flexible on-the-fly querying in SQLite database.
  4. The File I/O functions for more complex imports across multiple formats, including but not limited to CSV files.

Below we provided a table to give you a more in-depth overview of each method.

MethodDescriptionProsConsIdeal UseSource
1. DBeaver GUIUtilize the DBeaver database client to import CSV files into a SQLite database using GUI.- User-friendly.
- Visual tools for data mapping and schema design.
- Supports data transformation during import.
- Requires installation and setup of DBeaver.
- Slower than command
-line method for very large files.
- Consumes more system resources.
For users who prefer a graphical interface and need to import CSV files with custom data mappings or transformations.DBeaver Documentation
2. .import Command in SQLiteSimple command-line option for quick CSV imports.- Fast and straightforward.
- No need for pre-creating a table.
- Can skip headers with --skip 1.
- Limited control over schema.
- No data transformation or validation.
- Requires clean, structured CSV format.
For quick, basic CSV imports when you need minimal setup..import Command Documentation
3. CSV Virtual Table in SQLiteAllows querying CSV files as if they were part of the database, without importing data.- No import required.
- Query directly from CSV.
- Flexible for temporary data use or external datasets.
- Does not store data in the database.
- Performance can be slower on large datasets compared to actual imports.
For querying huge CSV on the fly without needing to persist data.CSV Virtual Table Documentation
4. File I/O Functions in SQLiteAdvanced approach using functions like readfile() to manually process and import CSV or other file formats.- Full control over data import.
- Supports multiple file formats.
- Custom processing and validation are possible.
- Requires more setup.
- More complex compared to .import.
For complex or multi-format imports where data needs to be processed, validated, or manipulated before import.File I/O Functions Documentation

Now that we've outlined the available methods, let's focus on the first two—they're the simplest and cover the most common use cases. We'll start by using the DBeaver database client for a graphical approach, then explore SQLite’s .import command for command-line efficiency. You can learn more about the two other methods through their documentation.

DBeaver is available for all major platforms (Windows, macOS, and Linux). Before we begin, ensure you have it installed—you can download it from the official website. For this guide, we'll be working with the Windows version.

You don’t need to install SQLite separately—DBeaver will prompt you with a pop-up to install the necessary drivers when you create an SQLite database.

  • Start by opening DBeaver and creating a connection for your SQLite database. From the main menu, click Database > New Database Connection. DBeaver interface showing the “New Database Connection” option under the “Database“ menu.
  • Choose SQLite from the list. DBeaver's “Connect to a database“ window showing the selection of “SQLite“.
  • Select the Create option, then choose where you'd like to save your new SQLite database file. At the last step, before clicking Finish you can test the connectivity of your database by pressing the Test Connection button at the bottom-left. DBeaver's “Connection Settings” window for SQLite, showing the file path to database file.
  • Once connected, your SQLite database will appear in the left sidebar, open the database tree to see Tables and other sections. DBeaver interface displaying the connected SQLite database, with an expanded tree showing database objects such as tables.

You can create a table that matches your file's structure before importing the CSV. While it's not mandatory, this step gives you more control and precision over the import process and field mapping.

  • In DBeaver, go to SQL Editor > New SQL Script. DBeaver interface showing the context menu for a SQLite database, with the “SQL Editor” option expanded to create or open SQL scripts.
  • As an example, we’ll use a CSV file containing id, name, and email columns as our test file. Run the below SQL script to create an equivalent table named users in SQLite, then press Execute button to run it. DBeaver interface displaying an SQL script to create “users” table with columns for id, name, and email.
  • You should now see the new table appear in the tables section. If it doesn’t show up right away, simply right-click on the database and refresh it. Once it’s visible, double-click on it to view the structure of the users table, just as defined. SQLite database structure displayed in DBeaver database client after a successful connection. For more information on data types, check out the official documentation.
  • With the table set, right-click on it and choose Import Data. Select your CSV file, map the columns (if necessary), and adjust any settings as needed. Once done, click Start to import the data. DBeaver interface showing the “Import Data” option for the “users” table in a SQLite database. For more options in the import process, you can see the DBeaver Data Import documentation.

The SQLite's Command Line Interface (CLI) is a powerful tool that allows you to perform database operations efficiently. In my experience, the CLI method is much faster for large files. When I imported a 500MB CSV file containing 11 million rows, the CLI completed the task in just 24 seconds, twice as fast as the DBeaver import wizard. Here’s how the SQLite's .import command works:

  • No table needed: SQLite’s .import command can auto-create a table from the CSV’s first row if it includes headers, so defining a table beforehand isn’t required. However, manually specifying the table structure gives you more control over the import process.
  • Headers: Use the --skip 1 option to treat the first row as headers and avoid importing them as data.
  • Handling Extra Columns: If you define the table beforehand and the CSV file has more columns than the table, the extra columns will be ignored. If the CSV has fewer columns, SQLite fills the missing values with NULL. To prevent data misalignment, make sure the columns in your CSV match the order and data types in your SQLite table. Building on the previous section where we created a database (SQLiteCSV.db) and a table matching the users CSV file schema, let's now explain how to import a CSV file into this database using the SQLite CLI.

Why Install SQLite on Windows? Installing SQLite lets you use the command line for faster data imports, especially for large datasets.

  1. Download the SQLite Tools package from the official website.
  2. Unzip the file and place sqlite3.exe in a convenient directory (e.g., C:\sqlite).

To use the features mentioned above, make sure you're running SQLite version 3.32.2 or later.

  • Launch the CLI: Press Win + R, type cmd, and hit Enter. Then navigate to the directory where you placed sqlite3.exe. If you saved it in C:\sqlite, you can navigate there by running:

    cd C:\sqlite
    
  • Open Your Database: Navigate to your database by running the below command.

    sqlite3 D:\SQLiteCSV.db
    

    If the database doesn't exist, this command will create it.

  • Set Import Mode: Tell SQLite you're importing a CSV file.

    .mode csv
    
  • Import the Data: Use the .import command to load your CSV into the desired table. Replace D:\users.csv with the path to your CSV file and users with your table name.

    .import --skip 1 D:\users.csv users
    

Run a quick query to ensure your data is imported correctly.

SELECT
  *
FROM
  users
    LIMIT 10;

After running this in your command line interface, you should see the first 10 records from your CSV file appear in your command line application.

After importing your CSV into SQLite, you can query the data in DBeaver. Right-click your SQLite database in the sidebar, select SQL Editor > New SQL Script to open a new script window, and run your query. For example, to group users by email domain, use this query on the users table:

SELECT
  SUBSTR( email, INSTR ( email, '@' ) + 1 ) AS domain,
    COUNT( * ) AS user_count
FROM
  users
GROUP BY
  domain
ORDER BY
  user_count DESC;

This query groups users by their Email domain and counts how many users are associated with each one. Here’s the result when we run this query against our sample data: DBeaver with a SQL query grouping users by email domain in the SQLite database.

For handling large CSV files, you can use SQLite's CLI for the best performance or choose a GUI based tool like DBeaver database client if you prefer a more user-friendly way. Both options allow you to efficiently import and query large CSV files without the complexity of traditional database systems. We encourage you to explore these methods and see how they simplify handling large CSV files. For more details on importing CSV files, check out SQLite documentation. Happy querying!