Python 脚本从数据库中提取数据并将其用于发送电子邮件

问题描述 投票:0回答:1

我正在尝试编写一个Python脚本,该脚本读取文件名,例如(COD_044916.pdf),在“COD_”之后和“.pdf”之前,其余文本是客户编号,该脚本应该在表中找到该客户编号甲骨文数据库。找到该客户编号所在的行后,应该从数据库中该表中该行的电子邮件列中获取该客户的电子邮件 ID。 然后通过该电子邮件发送此文件。

这是我尝试过的,但我想它不适用于 Oracle 11g。

import re
import oracledb

# Database connection details
db_username = "username"
db_password = "password"
db_dsn = "mis"

# Regular expression pattern to extract customer number from filenames
filename_pattern = r"COD_(\d+)\.pdf"

def extract_customer_number(filename):
match = re.search(filename_pattern, filename)
if match:
    return match.group(1)
return None

def get_customer_email_from_database(customer_number):
try:
    connection = oracledb.connect(user=db_username, password=db_password, dsn=db_dsn, config_dir='C:/app/client/Noman.Akhter/product/12.2.0/client_1/Network/Admin')
    cursor = connection.cursor()

    query = "SELECT t.int_addr FROM mis_customer t WHERE t.cus_no = :c_number"
    result = cursor.execute(query, c_number=customer_number).fetchone()

    cursor.close()
    connection.close()

    if result:
        return result[0]
    else:
        return None

except oracledb.DatabaseError as e:
    error_message = f"Error connecting to the database: {e}"
    raise Exception(error_message)

# Example filename
filename = "COD_044916.pdf"

try:
# Extract customer number from filename
customer_number = extract_customer_number(filename)
if customer_number:
    # Retrieve customer email from the database
    customer_email = get_customer_email_from_database(customer_number)
    if customer_email:
        print(f"Customer Number: {customer_number}")
        print(f"Customer Email: {customer_email}")
    else:
        print(f"Customer with number {customer_number} not found in the database.")
else:
    print("Filename does not match the expected pattern.")

except Exception as e:
print(f"An error occurred: {e}")
python oracle11g cx-oracle python-oracledb
1个回答
0
投票

如果您还没有尝试过,您可以考虑对数据库连接和游标使用 with 语句和上下文管理器。 即使在出现异常的情况下,这也应该确保正确关闭资源: (修改逻辑原子性和上下文)

import re
import cx_Oracle

# Database connection details
db_username = "username"
db_password = "password"
db_dsn = "hostname:port/service_name"  # Update with correct DSN

# Regular expression pattern to extract customer number from filenames
filename_pattern = r"COD_(\d+)\.pdf"

def extract_customer_number(filename):
    match = re.search(filename_pattern, filename)
    if match:
        return match.group(1)
    return None

def get_customer_email_from_database(customer_number):
    try:
        with cx_Oracle.connect(db_username, db_password, db_dsn) as connection:
            with connection.cursor() as cursor:
                query = "SELECT t.int_addr FROM mis_customer t WHERE t.cus_no = :c_number"
                result = cursor.execute(query, {"c_number": customer_number}).fetchone()

                if result:
                    return result[0]
                else:
                    return None

    except cx_Oracle.DatabaseError as e:
        error_message = f"Error connecting to the database: {e}"
        raise Exception(error_message)

# Example filename
filename = "COD_044916.pdf"

try:
    # Extract customer number from filename
    customer_number = extract_customer_number(filename)
    if customer_number:
        # Retrieve customer email from the database
        customer_email = get_customer_email_from_database(customer_number)
        if customer_email:
            print(f"Customer Number: {customer_number}")
            print(f"Customer Email: {customer_email}")
        else:
            print(f"Customer with number {customer_number} not found in the database.")
    else:
        print("Filename does not match the expected pattern.")

except Exception as e:
    print(f"An error occurred: {e}")

不确定这是否能让您到达您想去的地方。 否则,考虑实施更多错误日志记录以检查中断发生的位置(它可以连接到数据库吗?它可以连续检查客户编号吗?...)

另外,我会这么说,因为说总比不说好:

  • 确保您已安装
    cx_Oracle
    库 (
    pip install cx-Oracle
    )
  • 确保您已正确设置 Oracle 客户端库
© www.soinside.com 2019 - 2024. All rights reserved.