MacLochlainns Weblog

Michael McLaughlin's Technical Blog

Site Admin

Archive for the ‘Python Administrator’ tag

OracleDB Python Tutorial 1

without comments

This shows you how to get Python working with the Oracle Database 23c in Docker or Podman on Ubuntu. You can find useful connection strings for this in Oracle Database Free Get Started.

  1. First step requires you to install the pip3/span> utility on Ubuntu.

    sudo apt install -y python3-pip

  2. Second step requires that you pip3 install the oracledb library:

    sudo pip3 install oracledb --upgrade

  3. Third step requires you write a Python program to test your connection to Oracle Database 23c Free, like:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    #!/usr/bin/python
     
    # Import the Oracle library.
    import oracledb
     
    try:
      # Create a connection to local Docker or Podman installation.
      db = oracledb.connect(user='c##student', password='student', dsn='localhost:51521/FREE')
     
      # Print a connection message.
      print("Connected to the Oracle", db.version, "database.")
     
    except oracledb.DatabaseError as e:
      error, = e.args
      print(sys.stderr, "Oracle-Error-Code:", error.code)
      print(sys.stderr, "Oracle-Error-Message:", error.message)
     
    finally:
      # Close connection. 
      db.close()

    The 51521 port is the recommended port when setting up Docker or Podman services, however, it can be set to any port above 1024.

    It should print:

    Connected to the Oracle 23.3.0.23.9 database.
  4. Fourth step requires you write a Python program to test querying data from an Oracle Database 23c Free instance. I created the following avenger table and seeded it with six Avengers.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    /* Conditionally drop the table. */
    DROP TABLE IF EXISTS avenger;
     
    /* Create the table. */
    CREATE TABLE avenger
    ( avenger_id      NUMBER
    , first_name      VARCHAR2(20)
    , last_name       VARCHAR2(20)
    , character_name  VARCHAR2(20));
     
    /* Seed the table with data. */
    INSERT INTO avenger VALUES (1,'Anthony','Stark','Iron Man');
    INSERT INTO avenger VALUES (2,'Thor','Odinson','God of Thunder');
    INSERT INTO avenger VALUES (3,'Steven','Rogers','Captain America');
    INSERT INTO avenger VALUES (4,'Bruce','Banner','Hulk');
    INSERT INTO avenger VALUES (5,'Clinton','Barton','Hawkeye');
    INSERT INTO avenger VALUES (6,'Natasha','Romanoff','Black Widow');

    Then, I extended the program logic to include a cursor and for loop to read the values from the avenger table:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    
    #!/usr/bin/python
     
    # Import the Oracle library.
    import oracledb
     
    try:
      # Create a connection to local Docker or Podman installation.
      db = oracledb.connect(user='c##student', password='student', dsn='localhost:51521/FREE')
     
      # Create a cursor.
      cursor = db.cursor()
     
      # Execute a query.
      cursor.execute("SELECT   character_name " +
                     ",        first_name " +
                     ",        last_name " +
                     "FROM     avenger " +
                     "ORDER BY character_name")
     
      # Read the contents of the cursor.
      for row in cursor:
        print(row[0] + ':',row[2] + ',',row[1])
     
    except oracledb.DatabaseError as e:
      error, = e.args
      print(sys.stderr, "Oracle-Error-Code:", error.code)
      print(sys.stderr, "Oracle-Error-Message:", error.message)
     
    finally:
      # Close cursor and connection.
      cursor.close() 
      db.close()

    The 51521 port is the recommended port when setting up Docker or Podman services, however, it can be set to any port above 1024.

    It should print:

    Black Widow: Romanoff, Natasha
    Captain America: Rogers, Steven
    God of Thunder: Odinson, Thor
    Hawkeye: Barton, Clinton
    Hulk: Banner, Bruce
    Iron Man: Stark, Anthony
  5. Fifth step requires you write a Python program to test querying data filtered by a local variable from an Oracle Database 23c Free instance. This example looks only for the Hulk among the six Avengers.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    
    #!/usr/bin/python
     
    # Import the Oracle library.
    import oracledb
     
    try:
      # Create a connection to local Docker or Podman installation.
      db = oracledb.connect(user='c##student', password='student', dsn='localhost:51521/FREE')
     
      # Create a cursor.
      cursor = db.cursor()
     
      # Execute a query.
      stmt = "SELECT   character_name "            \
             ",        first_name "                \
             ",        last_name "                 \
             "FROM     avenger "                   \
             "WHERE    character_name = :avenger " \
             "ORDER BY character_name"
     
      # Execute with bind variable.
      cursor.execute(stmt, avenger = "Hulk")
     
      # Read the contents of the cursor.
      for row in cursor:
        print(row[0] + ':',row[2] + ',',row[1])
     
    except oracledb.DatabaseError as e:
      error, = e.args
      print(sys.stderr, "Oracle-Error-Code:", error.code)
      print(sys.stderr, "Oracle-Error-Message:", error.message)
     
    finally:
      # Close cursor and connection. 
      cursor.close() 
      db.close()

    It should print:

    Hulk: Banner, Bruce

As always, I hope this puts everything together for setting up Python with Oracle Database 23c Free.

Written by maclochlainn

December 10th, 2023 at 12:27 am

Install PyGame on Fedora

without comments

The PyGame library is a wonderful tool for building games with Python. It lets you accomplish a great deal by simply managing events. You need to understand how to use Python functions, modules, and events to build games with this Python library.

You can download and install the PyGame library with the yum utility like this:

yum install -y pygame

It should generate the following list when you install it as the root user:

Loaded plugins: langpacks, refresh-packagekit
Available Packages
pygame.x86_64                        1.9.1-14.fc20                        fedora
[root@localhost ~]# yum install -y pygame
Loaded plugins: langpacks, refresh-packagekit
Resolving Dependencies
--> Running transaction check
---> Package pygame.x86_64 0:1.9.1-14.fc20 will be installed
--> Processing Dependency: numpy for package: pygame-1.9.1-14.fc20.x86_64
--> Processing Dependency: libportmidi.so.0()(64bit) for package: pygame-1.9.1-14.fc20.x86_64
--> Processing Dependency: libSDL_ttf-2.0.so.0()(64bit) for package: pygame-1.9.1-14.fc20.x86_64
--> Processing Dependency: libSDL_mixer-1.2.so.0()(64bit) for package: pygame-1.9.1-14.fc20.x86_64
--> Processing Dependency: libSDL_image-1.2.so.0()(64bit) for package: pygame-1.9.1-14.fc20.x86_64
--> Running transaction check
---> Package SDL_image.x86_64 0:1.2.12-7.fc20 will be installed
---> Package SDL_mixer.x86_64 0:1.2.12-5.fc20 will be installed
--> Processing Dependency: libmikmod for package: SDL_mixer-1.2.12-5.fc20.x86_64
---> Package SDL_ttf.x86_64 0:2.0.11-4.fc20 will be installed
---> Package numpy.x86_64 1:1.8.2-2.fc20 will be installed
--> Processing Dependency: python-nose for package: 1:numpy-1.8.2-2.fc20.x86_64
---> Package portmidi.x86_64 0:217-9.fc20 will be installed
--> Running transaction check
---> Package libmikmod.x86_64 0:3.3.6-3.fc20 will be installed
---> Package python-nose.noarch 0:1.3.0-1.fc20 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
================================================================================
 Package            Arch          Version                  Repository      Size
================================================================================
Installing:
 pygame             x86_64        1.9.1-14.fc20            fedora         2.1 M
Installing for dependencies:
 SDL_image          x86_64        1.2.12-7.fc20            fedora          41 k
 SDL_mixer          x86_64        1.2.12-5.fc20            fedora          91 k
 SDL_ttf            x86_64        2.0.11-4.fc20            fedora          22 k
 libmikmod          x86_64        3.3.6-3.fc20             updates        142 k
 numpy              x86_64        1:1.8.2-2.fc20           updates        3.0 M
 portmidi           x86_64        217-9.fc20               fedora          26 k
 python-nose        noarch        1.3.0-1.fc20             fedora         272 k
 
Transaction Summary
================================================================================
Install  1 Package (+7 Dependent packages)
 
Total download size: 5.7 M
Installed size: 21 M
Downloading packages:
(1/8): SDL_image-1.2.12-7.fc20.x86_64.rpm                   |  41 kB  00:00     
(2/8): SDL_mixer-1.2.12-5.fc20.x86_64.rpm                   |  91 kB  00:00     
(3/8): portmidi-217-9.fc20.x86_64.rpm                       |  26 kB  00:00     
(4/8): SDL_ttf-2.0.11-4.fc20.x86_64.rpm                     |  22 kB  00:00     
(5/8): libmikmod-3.3.6-3.fc20.x86_64.rpm                    | 142 kB  00:00     
(6/8): numpy-1.8.2-2.fc20.x86_64.rpm                        | 3.0 MB  00:02     
(7/8): pygame-1.9.1-14.fc20.x86_64.rpm                      | 2.1 MB  00:01     
(8/8): python-nose-1.3.0-1.fc20.noarch.rpm                  | 272 kB  00:00     
--------------------------------------------------------------------------------
Total                                              1.7 MB/s | 5.7 MB  00:03     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction (shutdown inhibited)
  Installing : SDL_ttf-2.0.11-4.fc20.x86_64                                 1/8 
  Installing : SDL_image-1.2.12-7.fc20.x86_64                               2/8 
  Installing : portmidi-217-9.fc20.x86_64                                   3/8 
  Installing : libmikmod-3.3.6-3.fc20.x86_64                                4/8 
  Installing : SDL_mixer-1.2.12-5.fc20.x86_64                               5/8 
  Installing : python-nose-1.3.0-1.fc20.noarch                              6/8 
  Installing : 1:numpy-1.8.2-2.fc20.x86_64                                  7/8 
  Installing : pygame-1.9.1-14.fc20.x86_64                                  8/8 
  Verifying  : pygame-1.9.1-14.fc20.x86_64                                  1/8 
  Verifying  : SDL_mixer-1.2.12-5.fc20.x86_64                               2/8 
  Verifying  : python-nose-1.3.0-1.fc20.noarch                              3/8 
  Verifying  : libmikmod-3.3.6-3.fc20.x86_64                                4/8 
  Verifying  : 1:numpy-1.8.2-2.fc20.x86_64                                  5/8 
  Verifying  : portmidi-217-9.fc20.x86_64                                   6/8 
  Verifying  : SDL_image-1.2.12-7.fc20.x86_64                               7/8 
  Verifying  : SDL_ttf-2.0.11-4.fc20.x86_64                                 8/8 
 
Installed:
  pygame.x86_64 0:1.9.1-14.fc20                                                 
 
Dependency Installed:
  SDL_image.x86_64 0:1.2.12-7.fc20        SDL_mixer.x86_64 0:1.2.12-5.fc20      
  SDL_ttf.x86_64 0:2.0.11-4.fc20          libmikmod.x86_64 0:3.3.6-3.fc20       
  numpy.x86_64 1:1.8.2-2.fc20             portmidi.x86_64 0:217-9.fc20          
  python-nose.noarch 0:1.3.0-1.fc20      
 
Complete!

I hope this helps folks install the software.

Written by maclochlainn

March 25th, 2017 at 1:49 am