Interacting with table rows.
Checks if a row exists in the specified table that matches the given conditions.
def exists(database: str, table: str, conditions: dict, logical_operator: str = "AND") -> bool:
Parameter | Type | Description |
---|---|---|
database |
str | Name of the database |
table |
str | Name of the table |
conditions |
dict | Dictionary of field-value pairs to match |
logical_operator |
str | Logical operator for combining multiple conditions ("AND" or "OR") |
bool
- True if matching row exists, False otherwise
# Check if a user with username 'john_doe' exists
exists = Rows.exists("my_database", "users", {"username": "john_doe"})
print(exists) # Output: True or False
Retrieves rows from a table, optionally filtered by conditions.
def list(database: str, table: str, conditions: dict = None,
logical_operator: str = "AND", limit: int = 100) -> list[dict]:
Parameter | Type | Description |
---|---|---|
database |
str | Name of the database |
table |
str | Name of the table |
conditions |
dict | Optional dictionary of field-value pairs to filter rows |
logical_operator |
str | Logical operator for combining multiple conditions ("AND" or "OR") |
limit |
int | Maximum number of rows to return (default: 100) |
list[dict]
- A list of dictionaries, each representing a row
# 1. Retrieve all active users
database = "my_database"
table = "users"
conditions = {"status": "active"}
active_users = Rows.list(database, table, conditions, logical_operator="AND", limit=20)
print(active_users)
# Returns: [{'id': 1, 'username': 'alice', 'status': 'active'}, {'id': 2, 'username': 'bob', 'status': 'active'}]
# 2. Get all orders placed after March 1, 2024
database = "my_database"
table = "orders"
conditions = {"created_at >": "2024-03-01 00:00:00"}
recent_orders = Rows.list(database, table, conditions, logical_operator="AND", limit=50)
print(recent_orders)
# Returns: [{'id': 5, 'created_at': '2024-03-05', 'amount': 100.0}, ...]
# 3. Fetch all high-value transactions (amount > 1000)
database = "my_database"
table = "transactions"
conditions = {"amount >": 1000}
high_value_transactions = Rows.list(database, table, conditions, logical_operator="AND", limit=30)
print(high_value_transactions)
# Returns: [{'id': 10, 'amount': 1500, 'status': 'completed'}, ...]
# 4. Get orders with either status 'shipped' or 'delivered'
database = "my_database"
table = "orders"
conditions = {"status": "shipped", "status": "delivered"}
shipped_or_delivered_orders = Rows.list(database, table, conditions, logical_operator="OR", limit=100)
print(shipped_or_delivered_orders)
# Returns: [{'id': 20, 'status': 'shipped'}, {'id': 21, 'status': 'delivered'}, ...]
# 5. Retrieve users who haven't logged in for more than a year
database = "my_database"
table = "users"
conditions = {"last_login <": "2023-03-25 00:00:00"}
inactive_users = Rows.list(database, table, conditions, logical_operator="AND", limit=100)
print(inactive_users)
# Returns: [{'id': 7, 'username': 'charlie', 'last_login': '2022-12-10'}, ...]
Inserts one or more new rows into a table.
def create(database: str, table: str, data: list[dict]) -> bool:
Parameter | Type | Description |
---|---|---|
database |
str | Name of the database |
table |
str | Name of the table |
data |
list[dict] | List of dictionaries where each dictionary represents a row to insert |
bool
- True if all rows were inserted successfully, False otherwise
# Create two new users
success = Rows.create("my_database", "users", [
{"username": "alice", "email": "[email protected]"},
{"username": "bob", "email": "[email protected]"}
])
print("Creation successful:", success)
Deletes a specific row from a table by its ID.
def delete(database: str, table: str, row_id: int) -> bool:
Parameter | Type | Description |
---|---|---|
database |
str | Name of the database |
table |
str | Name of the table |
row_id |
int | The primary key ID of the row to delete |
bool
- True if the row was deleted successfully, False otherwise
# Delete user with ID 5
deleted = Rows.delete("my_database", "users", 5)
print("User deleted:", deleted)
Updates specific fields of a row in a table.
def update(database: str, table: str, row_id: int, update_data: dict) -> bool:
Parameter | Type | Description |
---|---|---|
database |
str | Name of the database |
table |
str | Name of the table |
row_id |
int | The primary key ID of the row to update |
update_data |
dict | Dictionary of field-value pairs to update |
bool
- True if the row was updated successfully, False otherwise
# Update email for user with ID 3
updated = Rows.update("my_database", "users", 3,
{"email": "[email protected]"})
print("Update successful:", updated)