Skip to content

Instantly share code, notes, and snippets.

@abdo1819
Last active March 14, 2020 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdo1819/bb9bc22f2b1afba6efcb599eb3138828 to your computer and use it in GitHub Desktop.
Save abdo1819/bb9bc22f2b1afba6efcb599eb3138828 to your computer and use it in GitHub Desktop.

python envirmoents

data base connection

cx_Oracle

good

  • official support from oracle
  • simple to use and excute queris
  • documintation very good with and lots of examples
  • sting manipulation is easier in python
  • can excute procedures storend in database need more search
  • use python sepcification api for db

bad

  • work only with oracledb
  • documintaion in procedures is minimal with some work in datatypes needed

code samples

excute SELECT query

import cx_Oracle
 
# Connect string format: [username]/[password]@//[hostname]:[port]/[DB service name]
conn = cx_Oracle.connect("system/GetStarted18c@//localhost:1521/XEPDB1")
cur = conn.cursor()
cur.execute("SELECT 'Hello World!' FROM dual")
res = cur.fetchall()
print(res)

insert batch of data

dataToInsert = [
    (10, 'Parent 10'),
    (20, 'Parent 20'),
    (30, 'Parent 30'),
    (40, 'Parent 40'),
    (50, 'Parent 50')
]
cursor.executemany("insert into ParentTable values (:1, :2)", dataToInsert)

procedure exution

in database

DECLARE
  l_customer_name customers.name%TYPE;
BEGIN
  -- get name of the customer 100 and assign it to l_customer_name
  SELECT name INTO l_customer_name
  FROM customers
  WHERE customer_id = 100;
  -- show the customer name
  dbms_output.put_line( v_customer_name );
END;

in python

outVal = cursor.var(int)
cursor.callproc('myproc', [123, outVal])

rest api

flask with Flask-RESTful

  • minimal server no restraction in project structure

code samples

hello world

#!flask/bin/python
from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "Hello, World!"

if __name__ == '__main__':
    app.run(debug=True)

return json

#!flask/bin/python
from flask import Flask, jsonify

app = Flask(__name__)

tasks = [
    {
        'id': 1,
        'title': u'Buy groceries',
        'description': u'Milk, Cheese, Pizza, Fruit, Tylenol', 
        'done': False
    }   
    ]

@app.route('/todo/api/v1.0/tasks', methods=['GET'])
def get_tasks():
    return jsonify({'tasks': tasks})

with url

tutorial

django

  • todo

crud demo

<repo link>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment