W↓
All docs
🔑
Sign Up/Sign In
docs.turso.tech/sdk/python/
Public Link
Apr 6, 2025, 5:08:28 PM - complete - 5.2 kB
Starting URLs:
https://docs.turso.tech/sdk/python/
## Page: https://docs.turso.tech/sdk/python/ 1 Retrieve database credentials You will need an existing database to continue. If you don’t have one, create one. Get the database URL: turso db show --url <database-name> Get the database authentication token: turso db tokens create <database-name> Assign credentials to the environment variables inside `.env`. TURSO_DATABASE_URL= TURSO_AUTH_TOKEN= You will want to store these as environment variables. 2 Install First begin by adding libSQL to your project: pip install libsql-experimental 3 Connect Then import the package: import libsql_experimental as libsql Now connect to your local or remote database using the libSQL connector: url = os.getenv("TURSO_DATABASE_URL") auth_token = os.getenv("TURSO_AUTH_TOKEN") conn = libsql.connect("hello.db", sync_url=url, auth_token=auth_token) conn.sync() conn = libsql.connect("hello.db") cur = conn.cursor() 4 Execute You can execute SQL queries against your existing database as follows: conn.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER);") conn.execute("INSERT INTO users(id) VALUES (10);") print(conn.execute("select * from users").fetchall()) 5 Sync (Embedded Replicas only) When using embedded replicas you should call `sync()` on the connector to sync your local database with the primary database. conn.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER);") conn.execute("INSERT INTO users(id) VALUES (1);") conn.commit() conn.sync() print(conn.execute("select * from users").fetchall()) --- ## Page: https://docs.turso.tech/sdk/python/quickstart 1 Retrieve database credentials You will need an existing database to continue. If you don’t have one, create one. Get the database URL: turso db show --url <database-name> Get the database authentication token: turso db tokens create <database-name> Assign credentials to the environment variables inside `.env`. TURSO_DATABASE_URL= TURSO_AUTH_TOKEN= You will want to store these as environment variables. 2 Install First begin by adding libSQL to your project: pip install libsql-experimental 3 Connect Then import the package: import libsql_experimental as libsql Now connect to your local or remote database using the libSQL connector: url = os.getenv("TURSO_DATABASE_URL") auth_token = os.getenv("TURSO_AUTH_TOKEN") conn = libsql.connect("hello.db", sync_url=url, auth_token=auth_token) conn.sync() conn = libsql.connect("hello.db") cur = conn.cursor() 4 Execute You can execute SQL queries against your existing database as follows: conn.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER);") conn.execute("INSERT INTO users(id) VALUES (10);") print(conn.execute("select * from users").fetchall()) 5 Sync (Embedded Replicas only) When using embedded replicas you should call `sync()` on the connector to sync your local database with the primary database. conn.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER);") conn.execute("INSERT INTO users(id) VALUES (1);") conn.commit() conn.sync() print(conn.execute("select * from users").fetchall()) --- ## Page: https://docs.turso.tech/sdk/python/reference ## 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: ### 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: ### Manual Sync The `Sync` function allows you to sync manually the local database with the remote counterpart: ## Encryption To enable encryption on a SQLite file, pass the encryption secret to the `encryption_key` option: --- ## Page: https://docs.turso.tech/sdk/python/examples * * GitHub Copilot Write better code with AI * GitHub Advanced Security Find and fix vulnerabilities * Actions Automate any workflow * Codespaces Instant dev environments * Issues Plan and track work * Code Review Manage code changes * Discussions Collaborate outside of code * Code Search Find more, search less * Explore * Learning Pathways * Events & Webinars * Ebooks & Whitepapers * Customer Stories * Partners * Executive Insights * * GitHub Sponsors Fund open source developers * The ReadME Project GitHub community articles * * Enterprise platform AI-powered developer platform * Pricing ## Provide feedback ## Saved searches ## Use saved searches to filter your results more quickly Sign up