SQLAlchemy Wrapper
This Python package provides a simple and intuitive wrapper for SQLAlchemy, making it easier to work with SQL databases in Python. It abstracts the complexities of direct SQLAlchemy usage, offering a streamlined API for database operations.
Features
Easy to use interface for database connections and operations. Supports executing custom SQL queries. Methods for fetching single or multiple records. Error handling for database operations.
Installation
To install the package, use pip:
pip install cd-sql
To get updates:
pip install --upgrade cd-sql
Usage
Connecting to a Database
from cd_sql import SQL
db = SQL('sqlite:///example.db')
db.connect()
Executing Queries
result = db.execute_query('SELECT * FROM my_table')
records = db.fetch_all('SELECT * FROM my_table')
record = db.fetch_one('SELECT * FROM my_table WHERE id = 1')
Disconnecting
db.disconnect()
This Python library provides a set of common SQL query utilities, making it easier to interact with databases using structured queries. It's designed to work with any database class that follows a specific interface for executing SQL commands.
Simplified methods for common SQL operations. Supports SELECT, INSERT, UPDATE, DELETE, and custom queries. Methods for table creation, deletion, and truncation. Counting rows in a table.
Usage
Basic Operations
from cd_sql import CommonQueries
db_instance = YourDatabaseClass() queries = CommonQueries(db_instance)
records = queries.select_all('my_table')
queries.insert('my_table', ['column1', 'column2'], ['value1', 'value2'])
queries.update('my_table', ['column1'], ['new_value'], 'id', 1)
queries.delete('my_table', 'id', 1)
Advanced Operations
row_count = queries.count_rows('my_table')
queries.create_table('new_table', ['id INTEGER PRIMARY KEY', 'name TEXT'])
queries.delete_table('old_table')
result = queries.custom_query('SELECT * FROM my_table WHERE column1 = ?', ['value1'])
Contributions to the package are welcome. Please follow the standard GitHub pull request process to propose changes.
This project is licensed under the MIT License - see the LICENSE file for details.
SQLite is a lightweight, file-based database. It doesn't require a separate server process.
from sql_class import SQL
# SQLite connection string format: 'sqlite:///path_to_database_file.db'
db = SQL('sqlite:///my_database.db')
db.connect()
MySQL is a popular open-source relational database management system.
from sql_class import SQL
# MySQL connection string format: 'mysql+pymysql://user:password@host:port/database'
db = SQL('mysql+pymysql://username:password@localhost/my_database')
db.connect()
PostgreSQL is an advanced open-source relational database.
from sql_class import SQL
# PostgreSQL connection string format: 'postgresql://user:password@host:port/database'
db = SQL('postgresql://username:password@localhost/my_database')
db.connect()
Microsoft SQL Server is a relational database management system developed by Microsoft.
from sql_class import SQL
# MS SQL Server connection string format: 'mssql+pyodbc://user:password@host:port/database'
db = SQL('mssql+pyodbc://username:password@localhost/my_database')
db.connect()
Oracle Database is a multi-model database management system produced and marketed by Oracle Corporation.
from sql_class import SQL
# Oracle connection string format: 'oracle+cx_oracle://user:password@host:port/?sid=database'
db = SQL('oracle+cx_oracle://username:password@localhost:1521/?sid=my_database')
db.connect()
- Replace
username
,password
,localhost
,port
, andmy_database
with your actual database credentials and details. - For MySQL, PostgreSQL, Microsoft SQL Server, and Oracle, you might need to install additional drivers or Python packages (like
pymysql
,psycopg2
,pyodbc
,cx_oracle
) to establish the connection. - The connection strings are formatted according to SQLAlchemy's URL format. Ensure that the format matches the version of SQLAlchemy you are using.
The core API of the SQL
class you've designed will not need to change to accommodate different databases like MySQL, PostgreSQL, Microsoft SQL Server, or Oracle. The flexibility of SQLAlchemy, which your class is based on, allows it to work with various databases without altering the primary interface for executing queries, connecting, or disconnecting.
However, there are a few considerations to keep in mind:
-
Database Drivers and Dependencies: While the API remains the same, the underlying database drivers required to connect to different databases vary. For instance, connecting to MySQL might require
pymysql
ormysql-connector-python
, PostgreSQL might needpsycopg2
, SQL Server might requirepyodbc
, and Oracle might needcx_Oracle
. These drivers should be installed separately as they are not included with SQLAlchemy. -
Connection Strings: The format of the connection string varies depending on the database. Users of your
SQL
class will need to provide the correct connection string format for the specific database they are connecting to. This is a user responsibility and does not require changes to your class. -
Database-Specific Features: Some databases have unique features or SQL syntax variations. While basic SQL operations (like SELECT, INSERT, UPDATE, DELETE) are generally consistent across databases, more advanced features or optimizations might differ. If your class or application needs to leverage these advanced features, additional methods or conditional logic might be required.
-
Error Handling: Different databases might throw different exceptions for similar error conditions. While SQLAlchemy does a good job of abstracting many of these, there might be cases where database-specific error handling is necessary.
-
Documentation and Examples: It would be beneficial to provide documentation or examples showing how to use your class with different databases, including the installation of necessary drivers and the correct format for connection strings.