A lightweight Oracle database connection handler for managing secure database interactions with support for environment variables and .env files.
This approach does not require cloning the repository as it installs the package directly from the remote repository. This is useful for users who only need to use the package without contributing to its development.
-
Create a Conda Environment, Install Package, and Activate Environment: Copy the
remote_install.yamllocated in the conda_env folder to your local machine.# remote_install.yaml name: lightoracle channels: - defaults dependencies: - python>=3.10 - pip: - git+https://github.com/GSU-Analytics/light_conn.git
Install the package by following these steps:
- Check that conda is installed by typing
conda -vin the command line. - Create a new conda environment using the
remote_install.yamlfile with the following command:conda env create -f remote_install.yaml conda activate lightoracle
- Check that conda is installed by typing
-
Install the Package in Existing Environment:
pip install git+https://github.com/GSU-Analytics/lightoracle.git
-
Install a Specific Version:
pip install git+https://github.com/GSU-Analytics/lightoracle.git@v0.2.0 -
Update the Package in Existing Environment:
pip install git+https://github.com/GSU-Analytics/lightoracle.git -U
-
Install with UV:
uv pip install git+https://github.com/GSU-Analytics/lightoracle.git
Or for a specific version:
uv pip install git+https://github.com/GSU-Analytics/lightoracle.git@v0.2.0
You can check the installed version:
from lightoracle import __version__
print(__version__) # e.g., "0.2.0"For local installation, particularly useful if you plan to contribute to the package or need a development setup:
-
Clone the Repository:
git clone https://github.com/GSU-Analytics/lightoracle.git cd lightoracle -
Create and Activate the Conda Environment: Use the
local_install.yamllocated in the conda_env folder to set up an environment with all necessary dependencies installed via Conda. Navigate to the directory containinglocal_install.yaml, or specify the full path to the file.conda env create -f local_install.yaml conda activate lightoracle
-
Install the Package Locally: This step installs the current local version of the package into the Conda environment.
pip install .
LightOracle supports two methods for configuring database credentials:
Create a .env file in your project root:
ORACLE_USER=your_username
ORACLE_DSN=your_dsn_here
ORACLE_LIB_DIR=/path/to/oracle/instant/client
ORACLE_PASSWORD=your_password_optionalThen use LightOracleConnection without arguments:
from lightoracle import LightOracleConnection
# Credentials loaded automatically from .env
conn = LightOracleConnection()
conn.test_connection()Notes:
ORACLE_PASSWORDis optional. If not provided, the package will use keyring (see below) or prompt for password.- Never commit your
.envfile to version control. - This method is backward compatible - you can still pass explicit arguments if needed.
Pass credentials directly when creating the connection:
from lightoracle import LightOracleConnection
conn = LightOracleConnection(
user="your_username",
dsn="your_dsn",
lib_dir="/path/to/oracle/instant/client"
)
conn.test_connection()LightOracle uses a three-tier approach for password management:
- Environment Variable (highest priority): Set
ORACLE_PASSWORDin your .env file - Keyring: Password stored securely in your system's keyring service
- Interactive Prompt: You'll be prompted to enter the password, which is then stored in keyring
To establish a connection to an Oracle database using the LightOracleConnection class, you can use either the .env configuration method (recommended) or the traditional config.py approach.
The config_example.py file contains essential attributes for setting up your Oracle database connection. Before you begin using the LightOracleConnection, make sure you have correctly configured the following attributes:
user: Your Oracle database username.dsn: The Data Source Name for the Oracle database connection, which typically includes the hostname, port, and database name.lib_dir: The directory path to the Oracle Client libraries on your machine. This is necessary if your Oracle Client libraries are not included in your system's PATH environment variable.
Here's an example of how to set up config_example.py:
# config.py
# Oracle Connection details
user = "your_username_here"
dsn = "your_dsn_here"
lib_dir = "C:/path/to/your/oracle/client/libraries" # Only set this if necessaryWith your config.py file set up, you can use the LightOracleConnection class to connect to your Oracle database. Below is a step-by-step example of importing your configuration and creating a database connection:
# main.py
from lightoracle import LightOracleConnection
from config import user, dsn, lib_dir
# Create a connection instance with the configured parameters
oracle_conn = LightOracleConnection(user, dsn, lib_dir)
# Test the Oracle database connection
oracle_conn.test_connection()
# Now you can use `oracle_conn` to perform database operations- Always ensure that the
user,lib_dir, anddsnattributes inconfig.pyare updated with the correct information corresponding to your Oracle database setup. - Never commit sensitive information, such as your actual Oracle username or DSN, to a public repository. It's recommended to use environment variables or a secure credential management system for handling sensitive data.
- If you encounter any issues with connecting to the Oracle database, verify that the Oracle Instant Client is correctly installed and configured on your system. You can find more information and a detailed installation guide on the Oracle Instant Client website.
See the example.py file for a simple example of how to use the LightOracleConnection class to connect to an Oracle database and execute a query. This will fetch the first 20 rows of student data from the edwprd.sdstumain table and save the results to a CSV file.
# example.py
from lightoracle import LightOracleConnection
from config import user, dsn, lib_dir
query = """
SELECT
s.term,
s.college,
s.department,
s.degree,
s.major,
s.gpa
FROM edwprd.sdstumain s
WHERE s.term = '202401'
AND s.major = 'PHY'
FETCH FIRST 20 ROWS ONLY
"""
oracle_conn = LightOracleConnection(user, dsn, lib_dir)
print("Connecting to Oracle database...")
df = oracle_conn.execute_query(query)
print("Query executed successfully.")
print("Saving query results to CSV file...")
df.to_csv('example.csv', index=False)
print("Results saved to example.csv.")See the reset_password_example.py file for an example of how to use the LightOracleConnection class to reset a user's password in an Oracle database. This script will prompt you to enter a new password and then test the connection with the new password.
# reset_password_example.py
from lightoracle import LightOracleConnection
from config import user, dsn, lib_dir
# Create a connection instance with your current credentials
conn = LightOracleConnection(user, dsn, lib_dir)
# Reset the password; you will be prompted to enter a new password
conn.reset_password()
# Test the connection with the newly set password
conn.test_connection()For more information, refer to the examples provided in lightoracle/oracle_connect.py for details on how to use this package.