Created
July 25, 2023 18:49
-
-
Save SU1199/efebf5d25dc312bf4be107d3f66c9aa5 to your computer and use it in GitHub Desktop.
Notion DB to SQLITE script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# quick and dirty way to convert notion db to an un-parsed sqlite db. | |
import requests | |
import json | |
import sqlite3 | |
DB_ID = "YOUR-DB-ID-HERE" | |
API_SECRET = "YOUR-API-KEY-HERE" | |
url_query = f"https://api.notion.com/v1/databases/{DB_ID}/query" | |
headers = { | |
'Authorization': f'Bearer {API_SECRET}', | |
'Notion-Version': '2022-06-28', | |
} | |
response_query = requests.post(url_query, headers=headers) | |
data_query = json.loads(response_query.text) | |
conn = sqlite3.connect('notion_table.db') | |
c = conn.cursor() | |
table_name = "NotionTable" | |
fields = data_query['results'][0]['properties'].keys() | |
field_string = ', '.join([f'{field} TEXT' for field in fields]) | |
c.execute(f'CREATE TABLE {table_name} ({field_string});') | |
values_string = ', '.join(['?' for _ in fields]) | |
for result in data_query['results']: | |
values = [str(result['properties'][field]) for field in fields] | |
c.execute(f'INSERT INTO {table_name} VALUES ({values_string});', values) | |
conn.commit() | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment