Package: cd-sql
SQLAlchemy WrapperThis 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.FeaturesEasy 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.InstallationTo install the package, use pip:pip install cd-sqlTo get updates:pip install --upgrade cd-sqlUsageConnecting to a Databasefrom cd_sql import SQLCreate an instance with the connection stringdb = SQL('sqlite:///example.db')Connect to the databasedb.connect()Executing QueriesExecute a custom queryresult = db.execute_query('SELECT * FROM my_table')Fetch all recordsrecords = db.fetch_all('SELECT * FROM my_table')Fetch a single recordrecord = db.fetch_one('SELECT * FROM my_table WHERE id = 1')DisconnectingDisconnect from the database when donedb.disconnect()Queriesnote: This is to be used in the SQL class and are helper methods to aid in your DB queries.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.FeaturesSimplified 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.UsageBasic Operationsfrom cd_sql import CommonQueriesdb_instance = YourDatabaseClass()
queries = CommonQueries(db_instance)Select all records from a tablerecords = queries.select_all('my_table')Insert a record into a tablequeries.insert('my_table', ['column1', 'column2'], ['value1', 'value2'])Update a record in a tablequeries.update('my_table', ['column1'], ['new_value'], 'id', 1)Delete a record from a tablequeries.delete('my_table', 'id', 1)Advanced OperationsCount rows in a tablerow_count = queries.count_rows('my_table')Create a new tablequeries.create_table('new_table', ['id INTEGER PRIMARY KEY', 'name TEXT'])Delete a tablequeries.delete_table('old_table')Execute a custom queryresult = queries.custom_query('SELECT * FROM my_table WHERE column1 = ?', ['value1'])More documentation at:Code DoctaContributingContributions to the package are welcome. Please follow the standard GitHub pull request process to propose changes.LicenseThis project is licensed under the MIT License - see the LICENSE file for details.Connecting examplesSQLiteSQLite is a lightweight, file-based database. It doesn't require a separate server process.fromsql_classimportSQL# SQLite connection string format: 'sqlite:///path_to_database_file.db'db=SQL('sqlite:///my_database.db')db.connect()MySQLMySQL is a popular open-source relational database management system.fromsql_classimportSQL# MySQL connection string format: 'mysql+pymysql://user:password@host:port/database'db=SQL('mysql+pymysql://username:password@localhost/my_database')db.connect()PostgreSQLPostgreSQL is an advanced open-source relational database.fromsql_classimportSQL# PostgreSQL connection string format: 'postgresql://user:password@host:port/database'db=SQL('postgresql://username:password@localhost/my_database')db.connect()Microsoft SQL ServerMicrosoft SQL Server is a relational database management system developed by Microsoft.fromsql_classimportSQL# MS SQL Server connection string format: 'mssql+pyodbc://user:password@host:port/database'db=SQL('mssql+pyodbc://username:password@localhost/my_database')db.connect()OracleOracle Database is a multi-model database management system produced and marketed by Oracle Corporation.fromsql_classimportSQL# 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()Notes:Replaceusername,password,localhost,port, andmy_databasewith your actual database credentials and details.For MySQL, PostgreSQL, Microsoft SQL Server, and Oracle, you might need to install additional drivers or Python packages (likepymysql,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 theSQLclass 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 requirepymysqlormysql-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 yourSQLclass 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.