Skip to main content

Database connection library for SQL Server

Project description

BaseConnect

Introduction

The BaseConnect library simplifies database operations for SQL Server by providing an easy-to-use Python interface. It abstracts repetitive tasks like connecting to the database, executing queries, and fetching data into pandas DataFrames. This package is built using pyodbc and pandas.

Installation

To use BaseConnect, ensure you have the following dependencies installed:

pyodbc: For database connection pandas: For data manipulation Install the dependencies using pip:

pip install pyodbc pandas

Include the BaseConnect package in your Python project structure.

Database Class

The Database class is the core component of the BaseConnect package. It provides methods for connecting to SQL Server, executing queries, and managing data.

Initialization

from baseconnect import Database
db = Database(
    server="server_name",
    database="database_name",
    user="username",                           # Optional, for SQL authentication
    password="password",                       # Optional, for SQL authentication
    driver="ODBC Driver 17 for SQL Server"     # Default driver
)
  • server (str): The SQL Server hostname or IP address.
  • database (str): The database name to connect to.
  • user (str): (Optional) The username for SQL authentication. Leave empty for Windows Authentication.
  • password (str): (Optional) The password for SQL authentication.
  • driver (str): (Optional) The ODBC driver to use. Defaults to "ODBC Driver 17 for SQL Server."

Methods


1. connect()

Establishes a connection to the SQL Server database.

db.connect()

Output: Prints a success or error message.


2. close()

Closes the database connection.

db.close()

Output: Prints a confirmation message when the connection is closed.


3. insert_row(row_data, table)

Inserts a new row into the specified table.

row_data = {"column1": value1, "column2": value2, ...}
table = "table_name"
db.insert_row(row_data, table)

Parameters:

row_data (dict): Dictionary where keys are column names and values are the respective data.

table (str): The table name where the row should be inserted.

Output: Prints a success or error message.


4. update_row(keys, updates, table)

Updates a row in the specified table.

keys = {"primary_key_column": value}
updates = {"column_to_update": new_value, ...}
table = "table_name"
db.update_row(keys, updates, table)

Parameters:

keys (dict): Dictionary of key-value pairs used in the WHERE clause.

updates (dict): Dictionary of column-value pairs to update.

table (str): The table name where the update should occur.

Output: Prints a success or error message.


5. execute_query(query)

Executes a custom SQL query.

query = "SELECT * FROM table_name WHERE column_name = 'value'"
results = db.execute_query(query)

Parameters:

query (str): The SQL query to execute.

Returns: List of tuples containing query results (if any).


6. get_table(table)

Fetches all rows from the specified table and returns them as a pandas DataFrame.

table = "table_name"
table_df = db.get_table(table)

Parameters:

table (str): The table name to fetch.

Returns: A pandas DataFrame containing all rows from the table.


7. query(query_string)

Executes a custom SQL query and returns the results as a pandas DataFrame.

query_string = "SELECT * FROM table_name WHERE column_name = 'value'"
query_df = db.query(query_string)

Parameters:

query_string (str): The SQL query to execute.

Returns: A pandas DataFrame containing the query results.


Best Practices

Always close the database connection using close() to release resources. Handle exceptions for invalid queries or data input to prevent SQL injection. Use environment variables to store sensitive information like database credentials.

Error Handling

The Database class includes basic error handling, which prints error messages for connection failures or query errors.

Conclusion

BaseConnect is a flexible and efficient library for managing SQL Server databases in Python. With its intuitive interface and pandas integration, it is ideal for applications requiring data manipulation and analytics.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

baseconnect-0.10.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

baseconnect-0.10-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file baseconnect-0.10.tar.gz.

File metadata

  • Download URL: baseconnect-0.10.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.13

File hashes

Hashes for baseconnect-0.10.tar.gz
Algorithm Hash digest
SHA256 127b18a9f8984bd01272e93f1bcfbc8917f86f3962c38be6f04e1f19c62d2cd5
MD5 f40f03e79ac65611a5a5347410b2a8d8
BLAKE2b-256 6a85ae2b84eb441fbb16f12bfb7c8bdb0a2bf928fcc1b40f3a581371c3d86f47

See more details on using hashes here.

File details

Details for the file baseconnect-0.10-py3-none-any.whl.

File metadata

  • Download URL: baseconnect-0.10-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.13

File hashes

Hashes for baseconnect-0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 1953956f8bb833debbf25364f7afa35b369e1307e7725f956c09605a82acc271
MD5 62489625d9b9ba348cb07d10a0808bb4
BLAKE2b-256 f34fb96630fc542d7e4cd98fa0621c1d353e31f0197873136e0ce10e20c1a899

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page