Skip to main content

Embedded Replicas

You can work with embedded replicas that can sync from the remote database to a local SQLite file, and delegate writes to the remote primary database:
import os  import libsql  conn = libsql.connect("local.db", sync_url=os.getenv("LIBSQL_URL"),  auth_token=os.getenv("LIBSQL_AUTH_TOKEN")) conn.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER);") conn.execute("INSERT INTO users(id) VALUES (1);") conn.commit()  print(conn.execute("select * from users").fetchall()) 
Embedded Replicas only works where you have access to the file system.

Periodic Sync

You can automatically sync at intervals by passing time in seconds to the sync_interval option. For example, to sync every minute, you can use the following code:
conn = libsql.connect("local.db", sync_interval=60, sync_url=os.getenv("LIBSQL_URL"),  auth_token=os.getenv("LIBSQL_AUTH_TOKEN")) 

Manual Sync

The Sync function allows you to sync manually the local database with the remote counterpart:
conn.execute("INSERT INTO users(id) VALUES (2);") conn.commit() conn.sync() 

Encryption

To enable encryption on a SQLite file, pass the encryption secret to the encryption_key option:
conn = libsql.connect("encrypted.db", sync_url=os.getenv("LIBSQL_URL"),  auth_token=os.getenv("LIBSQL_AUTH_TOKEN"),  encryption_key=os.getenv("ENCRYPTION_KEY")) 
Encrypted databases appear as raw data and cannot be read as standard SQLite databases. You must use the libSQL client for any operations — learn more.