Skip to content

Commit 0198114

Browse files
committed
Add examples
1 parent e77ae90 commit 0198114

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ To run all of these examples you can clone the entire repository to your disk. O
3131

3232
- **`query_execute.py`** connects to the `samples` database of your default catalog, runs a small query, and prints the result to screen.
3333
- **`insert_data.py`** adds a tables called `squares` to your default catalog and inserts one hundred rows of example data. Then it fetches this data and prints it to the screen.
34+
- **`transactions.py`** demonstrates multi-statement transaction support with explicit commit/rollback control. Shows how to group multiple SQL statements into an atomic unit that either succeeds completely or fails completely.
3435
- **`query_cancel.py`** shows how to cancel a query assuming that you can access the `Cursor` executing that query from a different thread. This is necessary because `databricks-sql-connector` does not yet implement an asynchronous API; calling `.execute()` blocks the current thread until execution completes. Therefore, the connector can't cancel queries from the same thread where they began.
3536
- **`interactive_oauth.py`** shows the simplest example of authenticating by OAuth (no need for a PAT generated in the DBSQL UI) while Bring Your Own IDP is in public preview. When you run the script it will open a browser window so you can authenticate. Afterward, the script fetches some sample data from Databricks and prints it to the screen. For this script, the OAuth token is not persisted which means you need to authenticate every time you run the script.
3637
- **`m2m_oauth.py`** shows the simplest example of authenticating by using OAuth M2M (machine-to-machine) for service principal.

examples/transactions.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from databricks import sql
2+
import os
3+
4+
with sql.connect(
5+
server_hostname=os.getenv("DATABRICKS_SERVER_HOSTNAME"),
6+
http_path=os.getenv("DATABRICKS_HTTP_PATH"),
7+
access_token=os.getenv("DATABRICKS_TOKEN"),
8+
) as connection:
9+
10+
# Disable autocommit to use explicit transactions
11+
connection.autocommit = False
12+
13+
with connection.cursor() as cursor:
14+
try:
15+
# Create tables for demonstration
16+
cursor.execute("CREATE TABLE IF NOT EXISTS accounts (id int, balance int)")
17+
cursor.execute(
18+
"CREATE TABLE IF NOT EXISTS transfers (from_id int, to_id int, amount int)"
19+
)
20+
connection.commit()
21+
22+
# Start a new transaction - transfer money between accounts
23+
cursor.execute("INSERT INTO accounts VALUES (1, 1000), (2, 500)")
24+
cursor.execute("UPDATE accounts SET balance = balance - 100 WHERE id = 1")
25+
cursor.execute("UPDATE accounts SET balance = balance + 100 WHERE id = 2")
26+
cursor.execute("INSERT INTO transfers VALUES (1, 2, 100)")
27+
28+
# Commit the transaction - all changes succeed together
29+
connection.commit()
30+
print("Transaction committed successfully")
31+
32+
# Verify the results
33+
cursor.execute("SELECT * FROM accounts ORDER BY id")
34+
print("Accounts:", cursor.fetchall())
35+
36+
cursor.execute("SELECT * FROM transfers")
37+
print("Transfers:", cursor.fetchall())
38+
39+
except Exception as e:
40+
# Roll back on error - all changes are discarded
41+
connection.rollback()
42+
print(f"Transaction rolled back due to error: {e}")
43+
raise
44+
45+
finally:
46+
# Restore autocommit to default state
47+
connection.autocommit = True

0 commit comments

Comments
 (0)