Python Oracle connection

How to start with Linux

Basic example of an Oracle connection using Python on Linux.

# Connect to an Oracle database

# Database info
username = 'username'
password = 'password'
dsn = 'localhost/SID'
port = 1512
encoding = 'UTF-8'

import cursor as cursor
import cx_Oracle
 
try:
    connection = cx_Oracle.connect(
        username,
        password,
        dsn,
        encoding=encoding)
    cur = connection.cursor()
 
    # Run the query
    cur.execute('select firstname, lastname from users where country ="Greece"')  # Just a simple examply
    for result in cur:
        print(result)
    cur.close()
 
except cx_Oracle.Error as error:
    print(error)
finally:
    # Close the connection
    if connection:
        connection.close()