Database Programming in Python:
Follow the steps to establish a connection between pthon application and oracle database:
1) Check your operating system 32 bit or 64bit.
2) If your operating system is 32 bit, then install python software also 32 bit. If your operating system is 64 bit then install python software also 64bit.
3) Download and Install Oracle Software also according to
your operating system.
4) Use the following at the command prompt to install cx_Oracle module:
C:\>pip install cx_Oracle
5) Write the following program in notepad or in IDLE
import cx_Oracle
con=cx_Oracle.connect("system/manager@localhost:1521/xe")
print("Connection Established Successfully")
con.close()
6) Run the above program
7) "system" is a username
8) "manager" is a password while installing oracle software specified.
9) "localhost" is a domain name. If the database software installed on same computer then use domain name as a localhost, otherwise use computer name.
10) "1521" is a port number. To check the port number open
"tnsnames.ora" file in the following directory:
C:\oraclexe\app\oracle\product\11.2.0\server\network\ADMIN
11) "xe" is a service id. To get the service id type the following
sql query at the SQL prompt.
SQL>select * from global_name;
12) To open sql prompt, Click on start button and type sqlplus
then press enter key.
Program to establish a connection between python application and oracle database:
import cx_Oracle
con=cx_Oracle.connect("system/manager@localhost:1521/xe")
print("Connection Established Successfully")
con.close()
Program to create a table:
import cx_Oracle
con=cx_Oracle.connect("system/manager@localhost:1521/xe")
cur=con.cursor()
cur.execute("create table student(rollno number(3), name varchar2(10), marks number(3)")
print("Table Created Successfully")
con.close()
Follow the steps to establish a connection between pthon application and oracle database:
1) Check your operating system 32 bit or 64bit.
2) If your operating system is 32 bit, then install python software also 32 bit. If your operating system is 64 bit then install python software also 64bit.
3) Download and Install Oracle Software also according to
your operating system.
4) Use the following at the command prompt to install cx_Oracle module:
C:\>pip install cx_Oracle
5) Write the following program in notepad or in IDLE
import cx_Oracle
con=cx_Oracle.connect("system/manager@localhost:1521/xe")
print("Connection Established Successfully")
con.close()
6) Run the above program
7) "system" is a username
8) "manager" is a password while installing oracle software specified.
9) "localhost" is a domain name. If the database software installed on same computer then use domain name as a localhost, otherwise use computer name.
10) "1521" is a port number. To check the port number open
"tnsnames.ora" file in the following directory:
C:\oraclexe\app\oracle\product\11.2.0\server\network\ADMIN
11) "xe" is a service id. To get the service id type the following
sql query at the SQL prompt.
SQL>select * from global_name;
12) To open sql prompt, Click on start button and type sqlplus
then press enter key.
Program to establish a connection between python application and oracle database:
import cx_Oracle
con=cx_Oracle.connect("system/manager@localhost:1521/xe")
print("Connection Established Successfully")
con.close()
Program to create a table:
import cx_Oracle
con=cx_Oracle.connect("system/manager@localhost:1521/xe")
cur=con.cursor()
cur.execute("create table student(rollno number(3), name varchar2(10), marks number(3)")
print("Table Created Successfully")
con.close()
Program to insert a record:
import cx_Oracle con=cx_Oracle.connect("system/manager@localhost:1521/xe")
cur=con.cursor()
cur=con.cursor()
cur.execute("insert into student values(1, 'aaa', 99)")
con.commit()
print("One Record Inserted Successfully")
con.close()
con.close()
Program to update a record:
import cx_Oracle con=cx_Oracle.connect("system/manager@localhost:1521/xe")
cur=con.cursor()
cur=con.cursor()
cur.execute("update student set marks=99 where rollno=1")
con.commit()
print("One Record Updated Successfully")
con.close()
con.close()
Program to delete a record:
import cx_Oracle con=cx_Oracle.connect("system/manager@localhost:1521/xe")cur=con.cursor()
cur.execute("delete from student where rollno=1")
con.commit()
print("One Record Deleted Successfully")
con.close()
con.close()