pip install cx_Oracle
C:\python>python Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
import os import cx_Oracle # Set folder in which Instant Client is installed in system path os.environ['PATH'] = 'c:\\oraclexe\\client' # Connect to hr account in Oracle Database 11g Express Edition con = cx_Oracle.connect("hr", "hr", "localhost/xe") print("Connected!") con.close()
import os import cx_Oracle os.environ['PATH'] = 'c:\\oraclexe\\client' con = cx_Oracle.connect("hr/hr@localhost") cur = con.cursor() cur.execute("insert into jobs values(:id,:title,:min,:max)", id='PYP', title='Python Programmer', min=5000, max=10000); print("Inserted Job Successfully!") cur.close() con.commit() con.close()
import os import cx_Oracle os.environ['PATH'] = 'c:\\oraclexe\\client' con = cx_Oracle.connect("hr/hr@localhost") cur = con.cursor() cur.execute("insert into jobs values(:1,:2,:3,:4)", ('PYP', 'Python Programmer', 5000, 10000)); print("Inserted Job Successfully!") cur.close() con.commit() con.close()
import os import cx_Oracle os.environ['PATH'] = 'c:\\oraclexe\\client' con = cx_Oracle.connect("hr/hr@localhost") cur = con.cursor() cur.execute("select * from jobs order by job_title") for job in cur: print(job[1]) cur.close() con.close()
create or replace procedure low_high_salaries(p_dept number, p_low out number, p_high out number) is begin select min(salary), max(salary) into p_low, p_high from employees where department_id = p_dept; end;
import os import cx_Oracle os.environ['PATH'] = 'c:\\oraclexe\\client' con = cx_Oracle.connect("hr/hr@localhost") cur = con.cursor() # Create output variables high = cur.var(int) low = cur.var(int) # Call stored procedure cur.callproc('low_high_salaries', (80, low, high)) # Print values of out variables - getvalue() returns value of variable print("Lowest is %d and highest is %d" % (low.getvalue(), high.getvalue())) cur.close() con.close()