Use Turso Database from Python via the pyturso package, a DB-API 2.0 compatible interface.
Turso Database is currently in BETA. It may contain bugs and unexpected behavior. Use caution with production data and ensure you have backups.
The pyturso package provides a DB-API 2.0 compatible interface for Turso Database. It is a drop-in replacement for the standard sqlite3 module and also ships an asyncio-compatible variant.
import tursoconn = turso.connect(":memory:")conn.execute("CREATE TABLE logs (id INTEGER, msg TEXT)")conn.execute("INSERT INTO logs VALUES (1, 'start'), (2, 'middle'), (3, 'end')")cur = conn.cursor()cur.execute("SELECT * FROM logs ORDER BY id")# Fetch one at a timefirst = cur.fetchone()print(first) # (1, 'start')# Fetch remainingrest = cur.fetchall()print(rest) # [(2, 'middle'), (3, 'end')]# Cursor is also iterablecur.execute("SELECT * FROM logs ORDER BY id")for row in cur: print(row)
Inspect column metadata via cursor.description:
cur.execute("SELECT id, msg FROM logs")column_names = [desc[0] for desc in cur.description]print(column_names) # ['id', 'msg']
By default, turso.connect() uses legacy transaction control that mirrors sqlite3: DML statements implicitly begin a transaction. Call conn.commit() to commit or conn.rollback() to abort.
import tursoconn = turso.connect(":memory:")conn.execute("CREATE TABLE accounts (id INTEGER PRIMARY KEY, balance INTEGER)")conn.execute("INSERT INTO accounts VALUES (1, 1000), (2, 500)")conn.commit()try: conn.execute("UPDATE accounts SET balance = balance - 100 WHERE id = 1") conn.execute("UPDATE accounts SET balance = balance + 100 WHERE id = 2") conn.commit() print("Transfer complete")except Exception as e: conn.rollback() print("Transfer failed:", e)finally: conn.close()
The Connection also works as a context manager. On __exit__ it commits on success and rolls back on exception:
with turso.connect(":memory:") as conn: conn.execute("CREATE TABLE t (x INTEGER)") conn.execute("INSERT INTO t VALUES (42)")# Commits automatically here
import tursoconn = turso.connect(":memory:")cur = conn.cursor()cur.execute(""" CREATE TABLE IF NOT EXISTS posts ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, content TEXT )""")posts = [ ("Hello, Turso!", "This is the first post."), ("Second post", "More content here."),]cur.executemany( "INSERT INTO posts (title, content) VALUES (?, ?)", posts)conn.commit()cur.execute("SELECT id, title FROM posts ORDER BY id")for row in cur: print(f"{row[0]}: {row[1]}")conn.close()