This project demonstrates how to use DuckDB in real projects.
DuckDB is an in-process SQL OLAP database management system. It is designed to support analytical query workloads, also known as Online Analytical Processing (OLAP). This README provides a guide on how to integrate and use DuckDB in your Python projects.
To install DuckDB, you can use pip:
pip install duckdb
import duckdb
# Connect to DuckDB
conn = duckdb.connect('example.db')
# Create a table
conn.execute('''
CREATE TABLE users (
id INTEGER,
name VARCHAR,
age INTEGER
)
''')
# Insert data
conn.execute('''
INSERT INTO users VALUES
(1, 'Alice', 30),
(2, 'Bob', 25),
(3, 'Charlie', 35)
''')
# Query data
result = conn.execute('SELECT * FROM users').fetchall()
print(result)
README.mdThis README.md file provides a basic introduction to DuckDB, installation instructions, usage examples, and a complete example project.
To use DuckDB in your Python project, follow these steps:
- Install DuckDB: Use the command
pip install duckdbto install the DuckDB Python package. - Import DuckDB: Import the DuckDB module in your Python script.
- Connect to DuckDB: Establish a connection to a DuckDB database file.
- Create Tables: Use SQL commands to create tables in the database.
- Insert Data: Insert data into the tables using SQL commands.
- Query Data: Execute SQL queries to retrieve data from the tables.
- Close Connection: Close the connection to the database when done.
- Example: See the example below for a complete DuckDB project.
- Example Project: The example project demonstrates how to create a DuckDB database, create tables, insert data, and query data using Python.
- Example Code: The example code is provided in the
example.pyfile. - Example Output: The example output shows the result of querying the data from the DuckDB database.
- Example Database: The example database file is named
example.db. - Example Table: The example table is named
users. - Example Data: The example data includes three users with their IDs, names, and ages.
- Example Query: The example query retrieves all data from the
userstable. - Example Result: The example result shows the output of the query, which includes the IDs, names, and ages of the users.
- Example Output: The example output is printed to the console.
- Example Output: The example output is shown below:
[(1, 'Alice', 30), (2, 'Bob', 25), (3, 'Charlie', 35)]The example project demonstrates how to use DuckDB in a real-world scenario. It includes creating a DuckDB database, creating tables, inserting data, and querying data using Python.
The example project is provided in the example.py file.
The example code is provided in the example.py file. It includes the following steps:
- Import the DuckDB module.
- Connect to a DuckDB database file.
- Create a table named
users. - Insert data into the
userstable. - Query data from the
userstable. - Print the query result.
- Close the connection to the database.
- Example Code:
import duckdb
# Connect to DuckDB
conn = duckdb.connect('example.db')
# Create a table
conn.execute('''
CREATE TABLE users (
id INTEGER,
name VARCHAR,
age INTEGER
)
''')
# Insert data
conn.execute('''
INSERT INTO users VALUES
(1, 'Alice', 30),
(2, 'Bob', 25),
(3, 'Charlie', 35)
''')
# Query data
result = conn.execute('SELECT * FROM users').fetchall()
print(result)
# Close the connection
conn.close()The example output shows the result of querying the data from the DuckDB database. The output is printed to the console. Example Output:
[(1, 'Alice', 30), (2, 'Bob', 25), (3, 'Charlie', 35)]The example database file is named example.db. It contains the users table with the following data:
| id | name | age |
|---|---|---|
| 1 | Alice | 30 |
| 2 | Bob | 25 |
| 3 | Charlie | 35 |
The example table is named users. It has three columns: id, name, and age. The table contains three rows of data.
The example data includes three users with their IDs, names, and ages. The data is inserted into the users table using SQL commands.
The example query retrieves all data from the users table. The query is executed using the SELECT statement.
The example result shows the output of the query, which includes the IDs, names, and ages of the users. The result is printed to the console.
The example output is shown below:
[(1, 'Alice', 30), (2, 'Bob', 25), (3, 'Charlie', 35)]The example output is printed to the console. It shows the result of querying the data from the DuckDB database.