Change language

Oracle Database Connection in Python

Oracle Database
To link any database with our Python program, we needed some kind of connector, which is nothing other than module cx_Oracle .

To install cx_Oracle:

 pip install cx_Oracle 

With this command you can install the cx_Oracle package , but first you need to install the Oracle database on your PC.

How to use this module to connect

  • Import a specific module database
    Ex. import cx_Oracle
  • connect (): now connect the Python program to the Oracle database using the connect () function.
     con = cx_Oracle.connect (’username / password @ localhost’) 
  • cursor (): to execute the sql query and provide the result, some special object is required — nothing but cursor () object
     cursor = cx_Oracle.cursor () 
  • execute method:

    cursor.execute (sqlquery) - - - -" to execute single query.
    cursor.execute (sqlqueries) - - - -" to execute a group of multiple sqlquery seperated by “;”

  • commit (): For a DML (Data Manuplate Language) query in this query, you have an operation (update, insert, delete), we need commit (), then only the result is reflected in the database.
  • Fetch (): fetches the next row from the query result set and returns one sequence, or None if there are no more rows available.
  • close (): after all submenus are done to close all operations
     cursor.close () con.close () 

Creting table:

# import module

import cx_Oracle 

 

 
# Create table in Oracle database

try :

 

con = cx_Oracle.connect ( ’scott / tiger @ localhost’ )

 

  # Now run sqlquery

cursor = con.cursor ()

 

# Creates a table in a roll with the title number

cursor.execute ("create table student (srollno number,

name varchar2 ( 10 ), efees number ( 10 , 2 ) ")

  

print ( "Table Created successful" )

 

except cx_Oracle.DatabaseError as e:

  print ( " There is a problem with Oracle " , e)

 
# finally writing if any error occurs
# then we can also close all database operations

finally :

if cursor:

  cursor.close ()

if con:

con.close ()

Exit :

 Table Created successful 

Insert into table:

# Program for creating a table in the Oracle database

import cx_Oracle

  

try :

 

con = cx_Oracle.connect ( ’scott / tiger @ localhost’ )

  

  # Now run sqlquery

cursor = con.cursor ()

cursor.execute ( "insert into student values ​​(19585, Niranjan Shukla, 72000" )

 

# the commit that inserts the supplied data

  con.commit ()

 

print ( "value inserted successful" )

 

except cx_Oracle.DatabaseError as e:

print ( "There is a problem with Oracle" , e)

  
# finally writing if any error occurs
# then we can also close everything database operations

finally < / code> :

if cursor:

cursor.close ()

if con:

con.close ()

Exit :

 value inserted successful 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically