Skip to content

Instantly share code, notes, and snippets.

@SU1199
Created July 25, 2023 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SU1199/efebf5d25dc312bf4be107d3f66c9aa5 to your computer and use it in GitHub Desktop.
Save SU1199/efebf5d25dc312bf4be107d3f66c9aa5 to your computer and use it in GitHub Desktop.
Notion DB to SQLITE script
# 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