Python

First follow the instructions to compile and install LiteSync from the source code or use the pre-compiled binaries for your platform. You can start with the free version.

Then install the litesync module according to the Python version:


Python 3

pip install litesync


Python 2.7

git clone --depth=1 https://gitlab.com/litesync/python2 litesync-python2

cd litesync-python2

python setup.py build

sudo python setup.py install


Example code

The litesync module has the same API as the sqlite3 module

import litesync as sqlite3
import json
import time

conn = sqlite3.connect('file:app.db?node=secondary&connect=tcp://server:port')

# check if the db is ready
while not conn.is_ready():
    time.sleep(0.250)

# now we can use the db connection
...


Update Notification

Your application can be notified when the local database is updated due to synchronization with remote nodes. The notification is done via a user-defined function.

def on_db_update(arg):
  # this is called by the worker thread
  # the application should NOT use the db connection here
  # the application can transfer the notification to the main thread
  print("update received")

conn.create_function("update_notification", 1, on_db_update)


Transaction Notification

Your application can be notified when local transactions are synchronized with remote nodes. The notification is done via a user-defined function. If the value in the `result` argument is not "OK" then it contains the error message.

def on_transaction_sync(sql, result):
  # this is called by the worker thread
  # the application should NOT use the db connection here
  # the application can transfer the notification to the main thread
  print(f"Transaction synchronized ({result}): {sql}")

conn.create_function("transaction_notification", 2, on_transaction_sync)


Synchronization Status

Your application can check the synchronization status of the local database with the remote nodes.

  status = conn.cursor().execute("PRAGMA sync_status").fetchone()[0]
  print("sync status:", status)

Important: The notification function is called by the worker thread. The application should NOT use the db connection inside the notification function and it must return as fast as possible! The application can transfer the notification to the main thread before returning.




Throubleshooting

We can also use the Python's sqlite3 module on Linux and Windows

On both cases the Python's sqlite3 module interfaces with the system's SQLite library


Linux

We have 2 options to make Python to use the modified SQLite library containing LiteSync:


A. Use the LD_LIBRARY_PATH when opening the application

LD_LIBRARY_PATH=/usr/local/lib/litesync python app.py

B. Make the sqlite3 module use the LiteSync library

sudo apt install patchelf

patchelf --replace-needed libsqlite3.so.0 liblitesync.so /usr/lib/python2.7/lib-dynload/_sqlite3.so

To check if it was successful:

ldd /usr/lib/python2.7/lib-dynload/_sqlite3.so


Windows

Replace the sqlite3.dll file in the folder \Python\DLLs with the one containing LiteSync (rename from litesync-0.1.dll to sqlite3.dll)


Usage

In this case we use the sqlite3 module instead of the litesync module

import litesync as sqlite3
import json
import time

conn = sqlite3.connect('file:app.db?node=secondary&connect=tcp://server:port')

# check if the db is ready
while not conn.is_ready():
    time.sleep(0.250)

# now we can use the db connection
...