Skip to content

Commit f345efa

Browse files
author
Peng Ren
committed
Update README
1 parent 577cc54 commit f345efa

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,27 @@ pip install -e .
6868

6969
## Quick Start
7070

71+
**Table of Contents:**
72+
- [Basic Usage](#basic-usage)
73+
- [Using Connection String](#using-connection-string)
74+
- [Context Manager Support](#context-manager-support)
75+
- [Using DictCursor for Dictionary Results](#using-dictcursor-for-dictionary-results)
76+
- [Cursor vs DictCursor](#cursor-vs-dictcursor)
77+
- [Query with Parameters](#query-with-parameters)
78+
- [Supported SQL Features](#supported-query-features)
79+
- [SELECT Statements](#select-statements)
80+
- [WHERE Clauses](#where-clauses)
81+
- [Nested Field Support](#nested-field-support)
82+
- [Sorting and Limiting](#sorting-and-limiting)
83+
- [INSERT Statements](#insert-statements)
84+
- [UPDATE Statements](#update-statements)
85+
- [DELETE Statements](#delete-statements)
86+
- [Transaction Support](#transaction-support)
87+
- [Apache Superset Integration](#apache-superset-integration)
88+
- [Limitations & Roadmap](#limitations--roadmap)
89+
- [Contributing](#contributing)
90+
- [License](#license)
91+
7192
### Basic Usage
7293

7394
```python
@@ -378,6 +399,33 @@ cursor.execute("DELETE FROM Music WHERE available = false")
378399
print(f"Deleted {cursor.rowcount} documents")
379400
```
380401

402+
### Transaction Support
403+
404+
PyMongoSQL supports DB API 2.0 transactions for ACID-compliant database operations. Use the `begin()`, `commit()`, and `rollback()` methods to manage transactions:
405+
406+
```python
407+
from pymongosql import connect
408+
409+
connection = connect(host="mongodb://localhost:27017/database")
410+
411+
try:
412+
connection.begin() # Start transaction
413+
414+
cursor = connection.cursor()
415+
cursor.execute('UPDATE accounts SET balance = balance - 100 WHERE id = ?', [1])
416+
cursor.execute('UPDATE accounts SET balance = balance + 100 WHERE id = ?', [2])
417+
418+
connection.commit() # Commit all changes
419+
print("Transaction committed successfully")
420+
except Exception as e:
421+
connection.rollback() # Rollback on error
422+
print(f"Transaction failed: {e}")
423+
finally:
424+
connection.close()
425+
```
426+
427+
**Note:** MongoDB requires a replica set or sharded cluster for transaction support. Standalone MongoDB servers do not support ACID transactions at the server level.
428+
381429
## Apache Superset Integration
382430

383431
PyMongoSQL can be used as a database driver in Apache Superset for querying and visualizing MongoDB data:

0 commit comments

Comments
 (0)