How to Read Records from Oracle Database Table using Python?
This post is part of a series of blog posts on basic CRUD operations in Oracle Database using python.
- CRUD operations in Oracle Database using Python
- How to Create Records into Oracle Database Table using Python?
- How to Read Records from Oracle Database Table using Python?
- How to Update and Delete Records from Oracle Database Table using Python?
This is the third post of this series. In the second post, we have already covered how to create records in the Oracle database table. In this post, let's understand the 'R' of the CRUD operations, which is how to read records from a database table.
To read the records from the database, we use the Select query. After executing the Select statement, we have to fetch the records from the database. For this operation, we have three methods:
- cursor.fetchone() : Used to fetch only one-row
- cursor.fetchall() : Used fetch all rows
- cursor.fetchmany(n) : Used to fetch n number of rows
Fetch Single Row
To fetch the records from the table, we first need to define the SELECT statement and then execute this statement using the execute() method. These are the same steps that we followed to insert the records. To fetch the single record from the table, we need to use the fetchone() method. This method fetches the first records from the result set of our query and returns them in the form of a tuple.
sql_stmt = """
SELECT * FROM EMP_TEST
"""
#execute the statement
cur.execute(sql_stmt)
#fetch the row
row = cur.fetchone()
As you can see in the above code snippet, I defined the Select statement to fetch the record from the EMP_TEST table. Then I executed that statement and fetched the single row using the fetchone() method. The row is returned in the form of a tuple and I stored it in a variable called row.
Here is the complete code block.
#fetchone.py
import cx_Oracle as cx
import db_config as db
try:
#create connection
conn = cx.connect(db.username, db.password,db.connect_string)
except Exception as err:
print('Error occured while trying to create a connection',err)
else:
try:
#create cursor
cur = conn.cursor()
sql_stmt = """
SELECT * FROM EMP_TEST
"""
#execute the statement
cur.execute(sql_stmt)
#fetch the row
row = cur.fetchone()
except Exception as err:
print('Error while fetching the records',err)
else:
print(row)
finally:
#close cursor
cur.close();
finally:
#close connection
conn.close()
Result:
Fetch All Rows
Using the fetchall() method, we can fetch all the rows from a table. This method fetches all the records from the result set of our query and returns them in the form of a list of tuples.
sql_stmt = """
SELECT * FROM EMP_TEST
"""
#execute the statement
cur.execute(sql_stmt)
#fetch all the rows
row = cur.fetchall()
As you can see in the above code snippet, I defined the Select statement to fetch the record from the EMP_TEST table. Then I executed that statement and fetched all the rows using the fetchall() method. The rows are returned in the form of a list of tuples and I stored it in a variable called row.
Here is the complete code block.
#fetchall.py
import cx_Oracle as cx
import db_config as db
try:
#create connection
conn = cx.connect(db.username, db.password,db.connect_string)
except Exception as err:
print('Error occured while trying to create a connection',err)
else:
try:
#create cursor
cur = conn.cursor()
sql_stmt = """
SELECT * FROM EMP_TEST
"""
#execute the statement
cur.execute(sql_stmt)
#fetch all the rows
row = cur.fetchall()
except Exception as err:
print('Error while fetching the records',err)
else:
print(row)
#work on data
for index,record in enumerate(row):
print('Index is ',index,' : ',record)
finally:
#close cursor
cur.close();
finally:
#close connection
conn.close()
Result:
Fetch n Rows
Using the fetchmany(n) method, we can fetch n number of rows from a table. Here n represents the number of rows we want to fetch from a table. This method fetches n records from the result set of our query and returns it in the form of a list of tuples.
sql_stmt = """
SELECT * FROM EMP_TEST
"""
#execute the statement
cur.execute(sql_stmt)
#fetch all the rows
row = cur.fetchmany(3)
As you can see in the above code snippet, I defined the Select statement to fetch the record from the EMP_TEST table. Then I executed that statement and fetched three rows using the fetchmany(3) method. The rows are returned in the form of a list of tuples and I stored it in a variable called row.
Here is the complete code block.
#fetchmany.py
import cx_Oracle as cx
import db_config as db
try:
#create connection
conn = cx.connect(db.username, db.password,db.connect_string)
except Exception as err:
print('Error occured while trying to create a connection',err)
else:
try:
#create cursor
cur = conn.cursor()
sql_stmt = """
SELECT * FROM EMP_TEST
"""
#execute the statement
cur.execute(sql_stmt)
#fetch the rows
row = cur.fetchmany(3)
except Exception as err:
print('Error while fetching the records',err)
else:
print(row)
#work on data
for index,record in enumerate(row):
print('Index is ',index,' : ',record)
finally:
#close cursor
cur.close();
finally:
#close connection
conn.close()
Result:
We now have a full understanding of how to perform Create and Read operations. In the next posting, we'll see the 'U' and 'D' of the CRUD operation, which is how to update and delete records from the database.
Comments
Post a Comment