Skip to main content

Python

This guide will walk you through the process of installing and using Mycelite in a Python project.

Prerequisites

Download the extension

curl -L https://github.com/mycelial/mycelite/releases/latest/download/aarch64-apple-darwin.tgz --output aarch64-apple-darwin.tgz
tar -xvzf aarch64-apple-darwin.tgz

Setup the writer

Import sqlite3

import sqlite3

Load the extension

The extension must be loaded before opening the database.

connection = sqlite3.connect(":memory:")
connection.enable_load_extension(True)
connection.execute("select load_extension('./libmycelite', 'mycelite_writer')")

Open the database

db = sqlite3.connect("writer.db")

Configure Mycelite

The first time you use Mycelite, you'll need to load the configuration extension and configure Mycelite, subsequently, you can skip this step.

db.enable_load_extension(True)
db.execute("select load_extension('./libmycelite', 'mycelite_config')")

Signup for a free account on the hub and retrieve your client id and secret.

Configure Mycelite by running the following insert statement. The domain is a slug-like unique name of your choosing. The client_id is your user name and the secret is obtained from the hub.

sql = """
insert into mycelite_config values
('domain', '<domain>/<db-name>'),
('client_id', '<your-hub-username>'),
('secret', '<secret-from-hub>');
"""

db.execute(sql)
db.commit()

Setup the reader

Import sqlite3

import sqlite3

Load the extension

The extension must be loaded before opening the database.

connection = sqlite3.connect(":memory:")
connection.enable_load_extension(True)
connection.execute("select load_extension('./libmycelite', 'mycelite_reader')")

Open the database

db = sqlite3.connect("reader.db")

Configure Mycelite

The first time you use Mycelite, you'll need to load the configuration extension and configure Mycelite, subsequently, you can skip this step.

db.enable_load_extension(True)
db.execute("select load_extension('./libmycelite', 'mycelite_config')")

Signup for a free account on the hub and retrieve your client id and secret.

Configure Mycelite by running the following insert statement. The domain is a slug-like unique name of your choosing. The client_id is your user name and the secret is obtained from the hub.

sql = """
insert into mycelite_config values
('domain', '<domain>/<db-name>'),
('client_id', '<your-hub-username>'),
('secret', '<secret-from-hub>');
"""

db.execute(sql)
db.commit()

Observing Synchronization

In the writer instance, create a table and then insert a row.

db.execute('create table test(number integer);')
db.execute('insert into test(number) values (42);')
db.commit()

# Give Mycelite time to sync before script exits
import time
time.sleep(5)

In the reader instance, you will see the new table and can query it.

# Give Mycelite time to sync before querying.
time.sleep(5)

result = db.execute('select * from test;').fetchall()
print(result)

Complete Snippets

Complete Writer Snippet

import sqlite3
import time


connection = sqlite3.connect(":memory:")
connection.enable_load_extension(True)
connection.execute("select load_extension('./libmycelite', 'mycelite_writer')")

db = sqlite3.connect("writer.db")

db.enable_load_extension(True)
db.execute("select load_extension('./libmycelite', 'mycelite_config')")

sql = """
insert into mycelite_config values
('domain', '<domain>/<db-name>'),
('client_id', '<your-hub-username>'),
('secret', '<secret-from-hub>');
"""

db.exec(sql)
db.commit()


db.execute('create table test(number integer);')
db.execute('insert into test(number) values (42);')
db.commit()

# Give Mycelite time to sync before script exits
import time
time.sleep(5)

Complete Reader Snippet

import sqlite3
import time

connection = sqlite3.connect(":memory:")
connection.enable_load_extension(True)
connection.execute("select load_extension('./libmycelite', 'mycelite_writer')")

db = sqlite3.connect("writer.db")

db.enable_load_extension(True)
db.execute("select load_extension('./libmycelite', 'mycelite_config')")
sql = """
insert into mycelite_config values
('domain', '<domain>/<db-name>'),
('client_id', '<your-hub-username>'),
('secret', '<secret-from-hub>');
"""

db.execute(sql)
db.commit()

# Give Mycelite time to sync before querying.
time.sleep(5)

result = db.execute('select * from test;').fetchall()
print(result)