Akron Constructor

Initialize database connections with the Akron class constructor for SQLite, MySQL, PostgreSQL, and MongoDB.

Overview

The Akron constructor is your entry point to any database. It accepts a connection URL string and automatically selects the appropriate driver based on the URL scheme.

Signature

Akron(db_url: str = "sqlite:///akron.db")

Parameters

db_url

Type: str

Default: "sqlite:///akron.db"

Database connection URL. The scheme determines which driver to use:

  • sqlite:// - SQLite database
  • mysql:// - MySQL database
  • postgres:// - PostgreSQL database
  • mongodb:// - MongoDB database

Returns

Type: Akron

An Akron instance configured with the appropriate database driver ready for operations.

Examples

SQLite Database

1from akron import Akron
2
3# Default SQLite database
4db = Akron()
5
6# Custom SQLite database
7db = Akron("sqlite:///my_app.db")
8
9# In-memory SQLite database
10db = Akron("sqlite:///:memory:")

MySQL Database

1from akron import Akron
2
3# MySQL connection
4db = Akron("mysql://user:password@localhost:3306/database")
5
6# MySQL with additional parameters
7db = Akron("mysql://user:password@localhost:3306/database?charset=utf8mb4")

PostgreSQL Database

1from akron import Akron
2
3# PostgreSQL connection
4db = Akron("postgres://user:password@localhost:5432/database")
5
6# PostgreSQL with SSL
7db = Akron("postgres://user:password@localhost:5432/database?sslmode=require")

MongoDB Database

1from akron import Akron
2
3# MongoDB connection
4db = Akron("mongodb://localhost:27017/database")
5
6# MongoDB with authentication
7db = Akron("mongodb://user:password@localhost:27017/database")

Connection URL Formats

SQLite

  • sqlite:///path/to/database.db - File database
  • sqlite:///:memory: - In-memory database
  • sqlite:///./relative/path.db - Relative path

MySQL

  • mysql://user:password@host:port/database - Standard format
  • mysql://user@host/database - No password
  • mysql://user:password@host/database?option=value - With options

PostgreSQL

  • postgres://user:password@host:port/database - Standard format
  • postgresql://user:password@host:port/database - Alternative scheme
  • postgres://user@host/database?sslmode=require - With SSL

MongoDB

  • mongodb://host:port/database - Simple connection
  • mongodb://user:password@host:port/database - With authentication
  • mongodb://host1:port1,host2:port2/database - Replica set

Error Handling

1from akron import Akron
2from akron.exceptions import AkronError
3
4try:
5 db = Akron("mysql://invalid:credentials@localhost/db")
6except AkronError as e:
7 print(f"Connection failed: {e}")
8 # Handle connection error
9
10# Check if database is connected
11if db.is_connected():
12 print("Database connected successfully")
13else:
14 print("Failed to connect to database")

Best Practices

Environment Variables

Store database credentials in environment variables for security:

1import os
2from akron import Akron
3
4# Using environment variables
5db_url = os.getenv("DATABASE_URL", "sqlite:///default.db")
6db = Akron(db_url)

Connection Pooling

Akron automatically handles connection pooling for database drivers that support it, ensuring efficient resource usage in production applications.

Database-Specific Notes

  • SQLite: Automatic creation of database file if it doesn't exist
  • MySQL: Requires mysql-connector-python package
  • PostgreSQL: Requires psycopg2 package
  • MongoDB: Requires pymongo package

Related Methods