Skip to content

A Python wrapper for interacting with Notion databases using SQL-style syntax

License

Notifications You must be signed in to change notification settings

aditya76-git/pynotiondb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 

Repository files navigation

pynotiondb

pynotiondb

A Python wrapper for interacting with Notion databases using SQL-style syntax


pynotiondb

pynotiondb is a Python package that provides a convenient way to interact with Notion databases using SQL-style syntax.

📋Details

⚙️Installation

Open your terminal or command prompt and enter the following command:

pip install git+https://github.com/aditya76-git/pynotiondb@main

Note: To use this package you need to have a Active Notion Account

🚀Pre-requisites

Before using this package, you'll need to set up a few things in your Notion workspace:

  1. Create an Integration in Notion: Begin by creating a new integration in Notion’s integrations dashboard: https://www.notion.com/my-integrations.
    DOCS

  2. Obtain Your API Secret: API requests require an API secret to be successfully authenticated. Visit the Secrets tab to get your integration’s API secret

  3. Get the Database ID: Retrieve the database ID from the URL of your Notion database.

For example, in the URL https://www.notion.so/f30ed4836a234308a63f7b76f71b098c?v=f9adf71ce9924344bf01e072150436cb, f30ed4836a234308a63f7b76f71b098c is the database ID

  1. Connect the Integration to the Database: Connect your database to the integration by clicking on the three dots menu, navigating to the connections tab, and selecting your integration from the available options.

📌 Note

  • To utilize this package, you'll initially need to create a database or table within Notion. Customize the table headers to align with your requirements; for instance, if you're managing customer data, you'd include headers such as "Name" and "Address" as needed.

  • When adding a new table header in the database, ensure to select "Text" or Number from the Type dropdown menu. This selection ensures that the data is stored as text or as number, which is compatible with the package's functionality for retrieving rows. Avoid selecting any other options from the dropdown menu.

  • As of now, the pynotiondb package only supports INSERT and SELECT statements. It does not offer functionalities to create tables or add table headers directly from the package itself. Therefore, users must manually create the tables with appropriate headers in Notion before using the package.

  • Even if you mistakenly input an incorrect table name while executing SQL statements, the query will still execute successfully due to the databaseId being used when you do mydb = NOTION_API("API_SECRET", "DATABASE_ID").

  • Additional statements will be implemented in future updates of the package.

📷 Notion Database View

Notion Database Web View

🚀Initialization

from pynotiondb import NOTION_API
mydb = NOTION_API("API_SECRET", "DATABASE_ID")

INSERT Statement

➡️ Single-Row Insertion

To insert a single row into the table:

sql = "INSERT INTO employees (name, address) VALUES (%s, %s)"
val = ("John", "Highway 21")
mydb.execute(sql, val)

➡️ Multiple-Row Insertion

To insert a single row into the table:

sql = "INSERT INTO employees (name, address) VALUES (%s, %s)"
val = [
    ("John", "Highway 21"),
    ("Lilly", "Road 99"),
]
mydb.execute(sql, val)

🔎 SELECT Statement

➡️ Default Retrieval with All Columns

To fetch data from the database with all columns and a default page size of 20:

sql = "SELECT * FROM employees"
data = mydb.execute(sql)
  • This query retrieves all rows and columns from the employees table.

  • The default page size is set to 20 rows.

  • Returned data includes all columns such as name, address, and salary.

{
  "data": [
    {
      "address": "Highway 21",
      "salary": 1000,
      "name": "John"
    },
    {
      "address": "Highway 21",
      "salary": 2000,
      "name": "John"
    }
  ],
  "next_cursor": null,
  "previous_cursor": null,
  "has_more": false
}

➡️ Retrieval with Specified Columns

To fetch data with specific columns:

sql = "SELECT name, address FROM employees"
data = mydb.execute(sql)
  • This query retrieves only the name and address columns from the employees table.

  • The default page size is set to 20 rows.

{
  "data": [
    {
      "address": "Highway 21",
      "name": "John"
    },
    {
      "address": "Highway 21",
      "name": "John"
    }
  ],
  "next_cursor": null,
  "previous_cursor": null,
  "has_more": false
}

➡️ Retrieval with Specified Columns and Custom Page Size

To fetch data with specific columns:

sql = "SELECT name, address FROM employees WHERE page_size = 1"
data = mydb.execute(sql)
sql = "SELECT * FROM employees WHERE page_size = 1"
data = mydb.execute(sql)
  • This query retrieves only the name and address columns from the employees table.

  • Adding * will select all the colums

  • The page_size parameter allows customization of the number of rows returned per page.

{
  "data": [
    {
      "name": "John",
      "address": "Highway 21"
    }
  ],
  "next_cursor": "7184fff3-8859-45a1-863b-ab5c0d403a45",
  "previous_cursor": null,
  "has_more": true
}

➡️ Applying Conditions

To apply conditions for data retrieval, such as filtering based on numeric values:

sql = "SELECT * FROM employees WHERE salary > 1000"
data = mydb.execute(sql)
  • This query retrieves all columns from the employees table where the salary is greater than 1000.

  • Only numeric columns can be used for numerical comparisons.

  • Make sure the property you are applying conditions for has a Number type in the Notion Database

{
  "data": [
    {
      "address": "Highway 21",
      "salary": 1291029102910219,
      "name": "John"
    },
    {
      "address": "Some Road",
      "salary": 10000000,
      "name": "Aditya"
    }
  ],
  "next_cursor": null,
  "previous_cursor": null,
  "has_more": false
}

➡️ Applying Conditions (2)

To apply conditions for data retrieval, such as filtering based on numeric values:

sql = "SELECT * FROM employees WHERE salary > 1000 AND name = 'John' and page_size = 10"
data = mydb.execute(sql)
  • This query retrieves all columns from the employees table where the salary is greater than 1000 and name which is John and page_size of 10.

  • Only numeric columns can be used for numerical comparisons.

  • Make sure the property you are applying conditions for has a Number type in the Notion Database

{
  "data": [
    {
      "address": "Highway 21",
      "salary": 1291029102910219,
      "name": "John"
    },
    {
      "address": "Some Road",
      "salary": 10000000,
      "name": "Aditya"
    }
  ],
  "next_cursor": null,
  "previous_cursor": null,
  "has_more": false
}

UPDATE Statement

➡️ Updating a Row

To update a single row into the table:

sql = "UPDATE employees SET salary = 20000 WHERE name = Rachel Adams"
sql = "UPDATE employees SET salary = 20000 WHERE name = 'Rachel Adams'"
mydb.execute(sql)
  • This query will update the salary to 20000 for the row with the name 'Rachel Adams'.
  • Using single quotes around the name is recommended, especially if the value contains spaces or special characters.

DELETE Statement

➡️ Single-Row Deletion

To delete a single row into the table:

sql = "DELETE FROM employees WHERE salary < 110"
mydb.execute(sql)

🌟 Show Your Support

  • If you find this project useful or interesting, please consider giving it a star on GitHub. It's a simple way to show your support and help others discover the project.

Github Stars

👨‍💻Developement

Thank you for your interest in contributing to this project! There are several ways you can get involved:

  • Opening Issues: If you encounter a bug, have a feature request, or want to suggest an improvement, please open an issue. We appreciate your feedback!
  • Cloning the Project: To work on the project locally, you can clone the repository by running:
git clone https://github.com/aditya76-git/pynotiondb.git
  • Sending Pull Requests: If you'd like to contribute directly to the codebase, you can fork the repository, make your changes, and then send a pull request. We welcome your contributions!

💻Authors

About

A Python wrapper for interacting with Notion databases using SQL-style syntax

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages