Integrating DuckDB with SQLite: A User-Friendly Guide

Nov 22, 2023 | Programming

Are you looking to harness the power of DuckDB while accessing data from your SQLite databases? If so, you’re in the right place! In this article, we’ll explore how to connect DuckDB to SQLite, read data, write data, and troubleshoot common issues.

Getting Started: Setting Up DuckDB with SQLite

The DuckDB SQLite extension allows seamless interaction between your DuckDB environment and SQLite databases. Think of DuckDB as a versatile Swiss Army knife, while SQLite is your reliable toolbox. Together, they help you manage datasets effectively.

Reading Data from SQLite

To begin accessing your SQLite database within DuckDB, you’ll need to use the ATTACH command:

ATTACH datadbsakila.db AS sakila;

By doing this, you can treat the tables in your SQLite database like they are standard DuckDB tables. The data is queried directly at runtime. This means if the tables are changed in the SQLite database, DuckDB will reflect those changes without needing additional commands.

Example of Reading Data

Once the database is attached, you can display all the tables:

SHOW TABLES;

You may query data through SQL as demonstrated below. Imagine collecting revenues from different movie categories, the following SQL fetches that information:

SELECT cat.name category_name, 
                                 Sum(Ifnull(pay.amount, 0)) revenue 
                          FROM   category cat 
                          LEFT JOIN film_category flm_cat 
                          ON cat.category_id = flm_cat.category_id 
                          LEFT JOIN film fil 
                          ON flm_cat.film_id = fil.film_id 
                          LEFT JOIN inventory inv 
                          ON fil.film_id = inv.film_id 
                          LEFT JOIN rental ren 
                          ON inv.inventory_id = ren.inventory_id 
                          LEFT JOIN payment pay 
                          ON ren.rental_id = pay.rental_id 
                          GROUP  BY cat.name 
                          ORDER  BY revenue DESC LIMIT  5;

Here, you can think of the SQL code as a chef combining various ingredients (tables) to create a delicious dish (final dataset). The ingredients come together through careful pairing (joins), resulting in a savory output: the revenue summary.

Opening SQLite Databases Directly

You can opt to open SQLite databases directly within DuckDB. This means that instead of using a DuckDB database file, you provide a path to your SQLite file directly upon connecting.

duckdb datadbsakila.db

When opened this way, DuckDB operates directly over the SQLite database, allowing you to access all tables without an ATTACH command.

Writing Data to SQLite

In addition to reading, DuckDB enables you to write back to SQLite databases. You can create new databases, tables and insert data:

ATTACH new_sqlite_database.db AS sqlite_db (TYPE SQLITE);
CREATE TABLE sqlite_db.tbl(id INTEGER, name VARCHAR);
INSERT INTO sqlite_db.tbl VALUES (42, 'DuckDB');

This functionality enhances your ability to export data to different formats or directly manipulate your SQLite databases using DuckDB commands.

Additional Operations Supported

  • Create Table: CREATE TABLE sqlite_db.tbl(id INTEGER, name VARCHAR);
  • Insert Data: INSERT INTO sqlite_db.tbl VALUES (42, 'DuckDB');
  • Copy Data: COPY sqlite_db.tbl TO data.parquet;
  • Update Data: UPDATE sqlite_db.tbl SET name='Woohoo' WHERE id=42;
  • Delete Data: DELETE FROM sqlite_db.tbl WHERE id=42;
  • Alter Table: ALTER TABLE sqlite_db.tbl ADD COLUMN k INTEGER;
  • Drop Table: DROP TABLE sqlite_db.tbl;
  • Create View: CREATE VIEW sqlite_db.v1 AS SELECT 42;
  • Transactions: BEGIN; ... ROLLBACK;

Troubleshooting Tips

While using DuckDB with SQLite, you may encounter some common issues. Here are a few troubleshooting suggestions:

  • Ensure your SQLite database path is correct; double-check for typos.
  • If you experience issues with reading data, verify the SQLite file permissions.
  • For performance concerns, consider indexing your SQLite tables.
  • Keep your DuckDB updated for latest features and improvements.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Building and Loading the Extension

To build the SQLite extension, type:

make

To run DuckDB, use the shell command:

.build/release/duckdb -unsigned

After launching DuckDB, load the SQLite extension with:

LOAD build/release/extension/sqlite_scanner/sqlite_scanner.duckdb_extension;

Conclusion

By integrating DuckDB with SQLite, you enhance your data management capabilities significantly. The ability to read, write, and execute complex queries opens up a world of possibilities within your data projects. Remember, with great power comes great responsibility. Keep your data secure and always back it up before running modifications.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox