How to Compare Data in a Text File with an Oracle Database Table Using Python

In this post, we’ll show you how to compare data in a text file with an Oracle database table using Python. You’ll learn how to read a text file, fetch data from a database, and match the data efficiently using simple steps.

Step 1: Read Data from a Text File

In Python, you can read files in different ways. The most common methods are using the built-in open() function or the with statement. Here’s a brief overview:

  • open() function: This function is used to open files in Python. It returns a file object that you can use to read or write to the file. However, you need to manually close the file after you’re done using it.

  • with statement: This is a more convenient way to handle file opening and closing. It automatically closes the file once the block of code is executed, making it a safer choice.

For this example, we’ll read a text file, extract words, and store them in a list for further processing.

Code to read the file from local system

Step 2: Create an Oracle Database Table

Let’s create a table in Oracle to store the data that we will later compare with the text file content. Use the following SQL script to create a table named pythonic_test with two columns: id (of type number) and text (of type varchar2).
 
Create Table with 2 column 
create table pythonic_test(id number(10),text varchar2(50));
 
Next, insert some sample data into this table:
 
Insert the value in the  created table using the below script 
insert into pythonic_test values(1,’My name is nitesh’);
insert into pythonic_test values(2,’My name is Athang’);
insert into pythonic_test values(15,’My name is Akshay’);
insert into pythonic_test values(16,’My name is akshay’);
insert into pythonic_test values(17,’My name is Prashant’);
 

Step 3: Connect Python to Oracle Database

To retrieve data from the Oracle database, you need to follow these steps:

1.Install cx_Oracle :This is a Python extension module that enables access to Oracle Database.By the below command, you can install cx_Oracle package but it is required to install Oracle database to use those functions.

pip install cx_Oracle

2.Establish a Connection: Use the connect() method from cx_Oracle to establish a connection with your Oracle database. You will need your database credentials (username, password, IP address, port, and database name).

3.Executing query: Use a cursor object to execute SQL queries. The cursor helps to interact with the database and fetch data. There are several ways to retrieve data:

  • fetchone(): Fetch a single row.
  • fetchmany(size): Fetch a specified number of rows.
  • fetchall(): Fetch all rows in the result set.

4.Commit and Close: If you perform any changes (e.g., insert or update operations), don’t forget to commit the transaction. Always close the connection to the database, ideally using a finally block to ensure the connection is closed even if an error occurs.

Let see the below snippet for a better understanding of the above concept.

Fetching value from the given table

Step 4: Compare Text File Data with Database Records

Below is a Python code snippet that splits the text from the database and stores each word in a list. It then compares the words in the database with those in the text file.

Checking the data with Oracle database to return thee matched values

Conclusion

In this post, you learned how to:

  • Read data from a text file.
  • Set up an Oracle database table.
  • Fetch data from an Oracle database using Python.
  • Compare the database records with the text file content.

For more on Oracle Python integration, refer to the official Oracle documentation. If you have any questions or suggestions, feel free to leave a comment below. Happy coding!!

You May Also Like

About the Author: Nitesh

I am a software engineer and Enthusiastic to learn new things

Leave a Reply

Your email address will not be published. Required fields are marked *