<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aum</title>
    <description>The latest articles on DEV Community by Aum (@aum_81621).</description>
    <link>https://dev.to/aum_81621</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2553687%2Fca0f4422-16f8-465e-a1dd-a53e590c083d.png</url>
      <title>DEV Community: Aum</title>
      <link>https://dev.to/aum_81621</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aum_81621"/>
    <language>en</language>
    <item>
      <title>Data Load through Python</title>
      <dc:creator>Aum</dc:creator>
      <pubDate>Tue, 10 Dec 2024 17:50:26 +0000</pubDate>
      <link>https://dev.to/aum_81621/data-load-through-python-4fp8</link>
      <guid>https://dev.to/aum_81621/data-load-through-python-4fp8</guid>
      <description>&lt;p&gt;Introduction&lt;/p&gt;

&lt;p&gt;Managing data is at the heart of many software applications. Often, we encounter scenarios where we need to import data from a CSV file into a database, such as Oracle. While tools like SQL*Loader exist, Python offers a flexible alternative for scripting and automation.&lt;/p&gt;

&lt;p&gt;In this tutorial, we’ll walk through a Python script that:&lt;br&gt;
    • Dynamically imports data from a CSV file.&lt;br&gt;
    • Handles CLOB (Character Large Object) data for the last column.&lt;br&gt;
    • Logs its execution details to a file for easier debugging and auditing.&lt;/p&gt;

&lt;p&gt;This script is designed for execution on Unix-based systems but can be adapted for other platforms.&lt;/p&gt;

&lt;p&gt;Prerequisites&lt;/p&gt;

&lt;p&gt;Before diving into the script, ensure the following:&lt;br&gt;
    1.  Python Environment: Python 3.6 or later installed.&lt;br&gt;
    2.  cx_Oracle Library: Install this package for Oracle database connectivity:&lt;/p&gt;

&lt;p&gt;pip install cx_Oracle&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3.  Oracle Instant Client: Required for database interaction. Ensure it’s installed and configured on your system. Set environment variables like LD_LIBRARY_PATH and TNS_ADMIN.
4.  Database Table Setup: Ensure your Oracle table matches the CSV structure:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;CREATE TABLE my_table (&lt;br&gt;
    column1 VARCHAR2(100),&lt;br&gt;
    column2 VARCHAR2(100),&lt;br&gt;
    column3 VARCHAR2(100),&lt;br&gt;
    column4 VARCHAR2(100),&lt;br&gt;
    column5 VARCHAR2(100),&lt;br&gt;
    column6 VARCHAR2(100),&lt;br&gt;
    column7 CLOB&lt;br&gt;
);&lt;/p&gt;

&lt;p&gt;The Python Script&lt;/p&gt;

&lt;p&gt;Here’s the full Python script:&lt;/p&gt;

&lt;p&gt;import os&lt;br&gt;
import sys&lt;br&gt;
import csv&lt;br&gt;
import cx_Oracle&lt;br&gt;
import logging&lt;br&gt;
from datetime import datetime&lt;/p&gt;

&lt;h1&gt;
  
  
  Define the log file location
&lt;/h1&gt;

&lt;p&gt;LOG_FILE = "/path/to/your/log/file/load_csv_to_oracle.log"&lt;/p&gt;

&lt;h1&gt;
  
  
  Set up logging
&lt;/h1&gt;

&lt;p&gt;logging.basicConfig(&lt;br&gt;
    filename=LOG_FILE,&lt;br&gt;
    filemode="a",&lt;br&gt;
    level=logging.INFO,&lt;br&gt;
    format="%(asctime)s - %(levelname)s - %(message)s"&lt;br&gt;
)&lt;/p&gt;

&lt;p&gt;def load_csv_to_oracle(file_name, table_name, oracle_connection_string):&lt;br&gt;
    """&lt;br&gt;
    Load data from a CSV file into an Oracle table.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Args:
    file_name (str): Path to the CSV file.
    table_name (str): Name of the Oracle table.
    oracle_connection_string (str): Oracle connection string in the format "user/password@host:port/service".
"""
# Check if the file exists
if not os.path.exists(file_name):
    logging.error(f"File '{file_name}' does not exist.")
    sys.exit(1)

try:
    # Connect to the Oracle database
    connection = cx_Oracle.connect(oracle_connection_string)
    cursor = connection.cursor()
    logging.info(f"Connected to Oracle database: {oracle_connection_string}")

    # Open the CSV file
    with open(file_name, 'r') as csv_file:
        reader = csv.reader(csv_file)
        headers = next(reader)  # Skip the header row

        # Validate column count
        if len(headers) != 7:
            logging.error(f"The CSV file must contain exactly 7 columns, but found {len(headers)}.")
            sys.exit(1)

        # Prepare the SQL insert query
        placeholders = ', '.join([':1', ':2', ':3', ':4', ':5', ':6', ':7'])
        insert_query = f"INSERT INTO {table_name} VALUES ({placeholders})"

        # Insert rows into the table
        row_count = 0
        for row in reader:
            if len(row) != 7:
                logging.warning(f"Skipping row due to incorrect column count: {row}")
                continue
            # Handle CLOB data for the last column
            clob_data = row[-1]
            cursor.execute(insert_query, row[:-1] + [clob_data])
            row_count += 1

    # Commit the transaction
    connection.commit()
    logging.info(f"Data loaded successfully into table '{table_name}'. Total rows inserted: {row_count}")

except cx_Oracle.DatabaseError as e:
    logging.error(f"Database error: {e}")
    sys.exit(1)
except Exception as e:
    logging.error(f"Error: {e}")
    sys.exit(1)
finally:
    if cursor:
        cursor.close()
    if connection:
        connection.close()
    logging.info("Database connection closed.")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;if &lt;strong&gt;name&lt;/strong&gt; == "&lt;strong&gt;main&lt;/strong&gt;":&lt;br&gt;
    # Validate the number of arguments&lt;br&gt;
    if len(sys.argv) != 3:&lt;br&gt;
        logging.error("Invalid number of arguments. Usage: python load_csv_to_oracle.py  ")&lt;br&gt;
        print("Usage: python load_csv_to_oracle.py  ")&lt;br&gt;
        sys.exit(1)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Get the arguments
file_name = sys.argv[1]
table_name = sys.argv[2]

# Oracle connection string
oracle_connection_string = input("Enter the Oracle connection string (user/password@host:port/service): ").strip()

# Log start
logging.info(f"Starting CSV load. File: {file_name}, Table: {table_name}")

# Load the CSV data into the Oracle table
load_csv_to_oracle(file_name, table_name, oracle_connection_string)

# Log end
logging.info("CSV load completed.")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Script Features&lt;br&gt;
    1.  Dynamic Input: Accepts the CSV file name and Oracle table name as command-line arguments.&lt;br&gt;
    2.  Error Handling: Gracefully handles file errors, database errors, and incorrect row formats.&lt;br&gt;
    3.  Logging: Logs all key events, warnings, and errors to a specified log file for auditing and debugging.&lt;br&gt;
    4.  CLOB Support: Handles large text data in the last column, ideal for long descriptions or notes.&lt;/p&gt;

&lt;p&gt;How to Use&lt;br&gt;
    1.  Save the script as load_csv_to_oracle.py.&lt;br&gt;
    2.  Execute it from the command line:&lt;/p&gt;

&lt;p&gt;python load_csv_to_oracle.py data.csv my_table&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3.  Provide the Oracle connection string when prompted.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Example Log Output&lt;/p&gt;

&lt;p&gt;2024-12-10 10:30:45 - INFO - Starting CSV load. File: data.csv, Table: my_table&lt;br&gt;
2024-12-10 10:30:46 - INFO - Connected to Oracle database: user/password@host:port/service&lt;br&gt;
2024-12-10 10:30:50 - INFO - Data loaded successfully into table 'my_table'. Total rows inserted: 100&lt;br&gt;
2024-12-10 10:30:50 - INFO - Database connection closed.&lt;br&gt;
2024-12-10 10:30:50 - INFO - CSV load completed.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;/p&gt;

&lt;p&gt;This script provides a robust solution for importing CSV data into Oracle while maintaining a clear audit trail through logging. Whether you’re an administrator or a developer, this approach is customizable and extensible for various data import tasks.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>micropython</category>
    </item>
  </channel>
</rss>
