Storage Areas
- As the Part of our Applications, we required to store our Data like Customers Information, Billing
- Information, Calls Information etc..
- To store this Data, we required Storage Areas. There are 2 types of Storage Areas.
- Temporary Storage Areas
- Permanent Storage Areas
Temporary Storage Areas
- These are the Memory Areas where Data will be stored temporarily.
Example
Python
Python objects like List, Tuple, Dictionary.
Output
PowerShell
6
- Once Python program completes its execution then these objects will be destroyed automatically and data will be lost.
Permanent Storage Areas
- Also known as Persistent Storage Areas. Here we can store Data permanently.
Example
Python
File Systems, Databases, Data warehouses, Big Data Technologies etc
File Systems
- File Systems can be provided by Local operating System. File Systems are best suitable to store very less Amount of Information.
Limitations
- We cannot store huge Amount of Information.
- There is no Query Language support and hence operations will become very complex.
- There is no Security for Data.
- There is no Mechanism to prevent duplicate Data. Hence there may be a chance of Data
- Inconsistency Problems.
- To overcome the above Problems of File Systems, we should go for Databases.
Databases
- We can store Huge Amount of Information in the Databases.
- Query Language Support is available for every Database and hence we can perform Database
- Operations very easily.
- To access Data present in the Database, compulsory username and pwd must be required.
- Hence Data is secured.
- Inside Database Data will be stored in the form of Tables. While developing Database Table
- Schemas, Database Admin follow various Normalization Techniques and can implement various
- Constraints like Unique Key Constrains, Primary Key Constraints etc which prevent Data
- Duplication. Hence there is no chance of Data Inconsistency Problems.
Limitations of Databases
- Database cannot hold very Huge Amount of Information like Terabytes of Data.
- Database can provide support only for Structured Data (Tabular Data OR Relational Data) and
- cannot provide support for Semi Structured Data (like XML Files) and Unstructured Data (like
- Video Files, Audio Files, Images etc)
- To overcome these Problems we should go for more Advanced Storage Areas like Big Data
- Technologies, Data warehouses etc.
Python Database Programming
- Sometimes as the part of Programming requirement we have to connect to the database and we
- have to perform several operations like creating tables, inserting data,updating data,deleting
- data,selecting data etc.
- We can use SQL Language to talk to the database and we can use Python to send those SQL
- commands to the database.
- Python provides inbuilt support for several databases like Oracle, MySql, SqlServer, GadFly, sqlite, etc.
- Python has seperate module for each database.
Example
Python
cx_Oralce module for communicating with Oracle database
pymssql module for communicating with Microsoft Sql Server
Output
PowerShell
5
Standard Steps for Python database Programming
- Import database specific module
Example
Python
import cx_Oracle
- Establish Connection between Python Program and database.
- We can create this Connection object by using connect() function of the module.
- con = cx_Oracle.connect(datbase information)
Example
Python
con=cx_Oracle.connect('scott/tiger@localhost')
- To execute our sql queries and to hold results some special object is required, which is nothing but Cursor object. We can create Cursor object by using cursor() method.
Example
Python
cursor=con.cursor()
- Execute SQL Queries By using Cursor object. For this we can use the following methods
- execute(sqlquery)  To execute a single sql query
- executescript(sqlqueries)  To execute a string of sql queries seperated by semi-colon ‘;’
- executemany()  To execute a Parameterized query
Example
Python
cursor.execute("select * from employees")
- commit or rollback changes based on our requirement in the case of DML Queries(insert|update|delete)
Example
Python
commit()  Saves the changes to the database
rollback()  rolls all temporary changes back
- Fetch the result from the Cursor object in the case of select queries
- fetchone()  To fetch only one row
- fetchall()  To fetch all rows and it returns a list of rows
- fecthmany(n)  To fetch first n rows
Example
Python
data =cursor.fetchone()
print(data)
Example
Python
data=cursor.fetchall()
for row in data:
print(row)
- close the resources
- After completing our operations it is highly recommended to close the resources in the reverse
- order of their opening by using close() methods
Example
Python
cursor.close()
con.close()
Note
- The following is the list of all important methods which can be used for python database
- programming.
- connect()
- cursor()
- execute()
- executescript()
- executemany()
- commit()
- rollback()
- fetchone()
- fetchall()
- fetchmany(n)
- fetch
- close()
- These methods won’t be changed from database to database and same for all databases.