Skip to main content

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.

Metadata

Release files for baseconnect 1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for baseconnect 1.3
File Size Uploaded
baseconnect-1.3.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for baseconnect 1.3
File Interpreter ABI Platform
baseconnect-1.3-py3-none-any.whl Python 3 none any Details

Total release size: 11.0 kB

Release files / baseconnect-1.3.tar.gz

Download URL baseconnect-1.3.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
5c33437118672bf8d0a7f7e77bd5d0165860d050c640c7d31ccdc1f56127e11f
BLAKE2b-256 checksum
How to use checksums
eac2915a2322c5dc51d512e0dc70be7357ab1b0b6445b6689cbe84eab794069d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.13

Release files / baseconnect-1.3-py3-none-any.whl

Download URL baseconnect-1.3-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7ed8334555dac8b7e3f6994c5afc384cdaf26d71523b7cdccfa9f65a7dbda141
BLAKE2b-256 checksum
How to use checksums
b96519e95edd6ea3ec6ed57083930dd6b148737f0bd35670dba3ee7685129118
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.13

Release history Release notifications | RSS feed

This release

1.3 This release

2 release files

1.2

2 release files

1.1

2 release files

0.138

2 release files

0.137

2 release files

0.136

2 release files

0.135

2 release files

0.134

2 release files

0.133

2 release files

0.132

2 release files

0.131

2 release files

0.13

2 release files

0.12

2 release files

0.11

2 release files

0.10

2 release files

0.8

2 release files

0.7.5

2 release files

0.7

2 release files

0.6

2 release files

0.5

2 release files

0.4

2 release files

0.3

2 release files

0.2

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page