Blog

Python Database

  • (4.0)
  •   |   344 Ratings

Python Database

Object-relational database management system (ORDBMS) are software, which stores the data in a permanent manner

Object-relational database management system allows performing the ‘random read’ and ‘ random write’ operations on the data of the ORDBMS

ORDBMS provides the security for the data

Online transaction processing (OLTP) operations implementations purpose we have to use ORDBMS

Difference ORDBMS are

  • ORACLE
  • MYSQL DB2
  • SQLITE3
  • SQL SERVER
  • TERA DATA
Learn how to use Python, from beginner basics to advanced techniques, with online video tutorials taught by industry experts. Enroll for Free Python Training Demo!

To provides the communication between the python program to the corresponding ORDBMS, we use databases related external modules. Databases related external modules we can install by using 
“PHP” applications
www.oracle.com
10G
11G
12C 
Download & install the oracle database 11g or 12c Enterprise edition
Username=SCOTT
Password=tiger
Service name (0r) logical database name (0r) SID or host storing name=oracle
Part no=1521
IP address=local host
Download & install ‘python 2.7 version”
Microsoft visual c++ compiler for python 2.7 version” download and install
Download & Install the “cx_oracle” module by using following command
Search in command prompt
c:/users/mindmajix>cd….
c:/users>cd….
c:/>cd python 2.7
c:/python 2.7 > cd scripts
c:/python 2.7/scripts>PIP install cx_oracle
We can establish connection with the database by calling connect function of cx_oracle module

Syntax:
Import cx_oracle
con=cx_oracle.connect (‘dbusername’, ‘dbpwd’,ipadd of the computer where database is installed: portno/ database service name’)
Connect function establish the connection with database with the given details, store the connection details into connection object and that object address will be stored into the given variable.
After establishing the connection with database to sent the sql queries to the database we have to create “cursor object”
Cursor objects can be created by calling cursor method of the connection object

Syntax:
cur=con.cursor()
By calling method of cursor objects, we can execute the sql queries or psql programs
By calling the methods of cursor objects we can read the data from the cursor object which is given by the database (sql) queries or psql programs
After reading the data from the cursor object we have to close the cursor object by calling close method of cursor object.
After closing the cursor object we have to close the connection object by calling close  method of connection object.

Example:

Import cx_oracle
con.cx_oracle.connect(‘scott’, ‘tiger’, ‘localhost:1521/orcl’)
cur=con.cursor()
cur.execute(“select*from dept”)
For row in cur:
Print (row)
cur.close()
con.close()

Output:

(10, ‘ACCOUNTING’, ‘NEW YORK’)
(20, ‘RESEARCH’, ‘DALLAS’)
(30, ‘SALES’, ‘CHICAGO’)
(40, ‘OPERATIONS’, ‘BATSON’)

Whenever connection is established with the database connection should get closed before terminating the program.
To close the connection if the connection is established we use exception handling techniques.

Explore Python Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Popular Courses in 2018

Get Updates on Tech posts, Interview & Certification questions and training schedules