Skip to main content

Database Programming

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()

Program to insert a record:
import cx_Oracle con=cx_Oracle.connect("system/manager@localhost:1521/xe")
cur=con.cursor()
cur.execute("insert into student values(1, 'aaa', 99)")
con.commit()
print("One Record Inserted Successfully")
con.close()

Program to update a record:
import cx_Oracle con=cx_Oracle.connect("system/manager@localhost:1521/xe")
cur=con.cursor()
cur.execute("update student set marks=99 where rollno=1")
con.commit()
print("One Record Updated Successfully")
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()


Popular posts from this blog

Python Fundamentals

Python: Python is an interpreted, high level, general purpose programming language. Differences between compiler and interpreter 1) Compiler converts whole program at a time where as Interpreter conver line by line 2) Compiler generates a file where as Interpreter does not generates a file 3) Compiler is fast where as Interpreter is slow Both Compiler and Interpreter are translation softwares. Whenever we run python program, internally source code converted into byte code by python compiler, byte code converted into bit code by python virtual machine and bit code executed under operating system to get the output. Python supports scripting, structured programming, modular programming and Object Oriented Programming. Applications of python: 1) Artificial Intelligence & Machine Learning Applications 2) Data Science & Data Visualization Applications 3) Web Scrapping Applications 4) Scientific & Numeric Applications 5) IOT(Internet Of Things) Applic

Control Flow Statements

Control Flow Statements are divided into three categories in Python: 1) Decision Making Statements 2) Iteration Statements(Loops) 3) Jump Statements 1) Decision Making Statements: Decision making statements contain conditions. If the condition is true then a set of statements executed and if the condition is false then another set of statements are executed. Decision making statements are also called selection statements. i) if Statement ii) if else Statement iii) if elif ....... else Statement iv) Nested if Statement 2) Iteration Statements(Loops): A set of statements executed repeatedly until the condition becomes false is called as iterative statement or loop. i) while loop ii) while loop with else block iii) for loop iv) for loop with else block v) Nested loops 3) Jump Statements: Jump statements are used to terminate the loop or to skip the part of a loop. i) break Statement ii) continue Statement iii) pass Statement Assignment4:

Collection Types

Collection Types: A collection is an object that represents group of objects. Advantages of Collection Types: 1) R educes programming effort 2) Increases programming speed and quality There are five collection types in python: 1) list 2) tuple 3) set 4) frozenset 5) dict List: Ø List can be created by using square brackets Ø List can also be created by using list() function Ø List can have homogeneous elements or heterogeneous elements Ø List allows duplicates Ø List supports both positive and negative indexing Ø List supports slice operations also Ø List is mutable Ø List supports deleting an element Ø List is an iterable object Ø Elements of a list can be iterated by using for loop or while loop Ø Insertion order is preserved in a list Examples: 1) List with homogeneous elements: a=[10,20,15,25,30] print(a) print(type(a)) 2) List with heterogeneous elements: a=[10, 10.2, “abc”, True] print(a) 3) List with duplicate elements(Ind