Python
This guide will walk you through the process of installing and using Mycelite in a Python project.
Prerequisites
- on OSX, this works best with the brew installed versions of sqlite and python
Download the extension
- Mac Arm
- Mac x86
- Linux Arm
- Linux x86 gnu
- Linux x86 musl
- Windows x86 gnu
- Windows x86 msvc
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
curl -L https://github.com/mycelial/mycelite/releases/latest/download/x86_64-apple-darwin.tgz --output x86_64-apple-darwin.tgz
tar -xvzf x86_64-apple-darwin.tgz
curl -L https://github.com/mycelial/mycelite/releases/latest/download/arm-unknown-linux-gnueabihf.tgz --output arm-unknown-linux-gnueabihf.tgz
tar -xvzf arm-unknown-linux-gnueabihf.tgz
curl -L https://github.com/mycelial/mycelite/releases/latest/download/x86_64-unknown-linux-gnu.tgz --output x86_64-unknown-linux-gnu.tgz
tar -xvzf x86_64-unknown-linux-gnu.tgz
curl -L https://github.com/mycelial/mycelite/releases/latest/download/x86_64-unknown-linux-musl.tgz --output x86_64-unknown-linux-musl.tgz
tar -xvzf x86_64-unknown-linux-musl.tgz
curl.exe -L https://github.com/mycelial/mycelite/releases/latest/download/x86_64-pc-windows-gnu.zip --output x86_64-pc-windows-gnu.zip
tar.exe -xvzf x86_64-pc-windows-gnu.zip
curl.exe -L https://github.com/mycelial/mycelite/releases/latest/download/x86_64-pc-windows-msvc.zip --output x86_64-pc-windows-msvc.zip
tar.exe -xvzf x86_64-pc-windows-msvc.zip
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)