Skip to content

anandtopu/duckdb_examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Project Title

This project demonstrates how to use DuckDB in real projects.

Introduction

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.

Installation

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.md

This README.md file provides a basic introduction to DuckDB, installation instructions, usage examples, and a complete example project.

Usage

To use DuckDB in your Python project, follow these steps:

  1. Install DuckDB: Use the command pip install duckdb to install the DuckDB Python package.
  2. Import DuckDB: Import the DuckDB module in your Python script.
  3. Connect to DuckDB: Establish a connection to a DuckDB database file.
  4. Create Tables: Use SQL commands to create tables in the database.
  5. Insert Data: Insert data into the tables using SQL commands.
  6. Query Data: Execute SQL queries to retrieve data from the tables.
  7. Close Connection: Close the connection to the database when done.
  8. Example: See the example below for a complete DuckDB project.
  9. Example Project: The example project demonstrates how to create a DuckDB database, create tables, insert data, and query data using Python.
  10. Example Code: The example code is provided in the example.py file.
  11. Example Output: The example output shows the result of querying the data from the DuckDB database.
  12. Example Database: The example database file is named example.db.
  13. Example Table: The example table is named users.
  14. Example Data: The example data includes three users with their IDs, names, and ages.
  15. Example Query: The example query retrieves all data from the users table.
  16. Example Result: The example result shows the output of the query, which includes the IDs, names, and ages of the users.
  17. Example Output: The example output is printed to the console.
  18. Example Output: The example output is shown below:
[(1, 'Alice', 30), (2, 'Bob', 25), (3, 'Charlie', 35)]

Example Project

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.

Example Code

The example code is provided in the example.py file. It includes the following steps:

  1. Import the DuckDB module.
  2. Connect to a DuckDB database file.
  3. Create a table named users.
  4. Insert data into the users table.
  5. Query data from the users table.
  6. Print the query result.
  7. Close the connection to the database.
  8. 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()

Example Output

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)]

Example Database

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

Example Table

The example table is named users. It has three columns: id, name, and age. The table contains three rows of data.

Example Data

The example data includes three users with their IDs, names, and ages. The data is inserted into the users table using SQL commands.

Example Query

The example query retrieves all data from the users table. The query is executed using the SELECT statement.

Example Result

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.

Example Output

The example output is shown below:

[(1, 'Alice', 30), (2, 'Bob', 25), (3, 'Charlie', 35)]

Example Output

The example output is printed to the console. It shows the result of querying the data from the DuckDB database.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages