Introduction
Today, I will take on a new part of
Python for my tutorial series. In this part, you will learn about Database Handling in Python.
I already told you about the following:
Python language supports many database servers as:
- MySQL
- PostgreSQL
- Microsoft SQL Server
- Oracle
- SQLite
Note
We can work with any database without changing code because mostly the Database works with the same method for handling databases that give flexibility to change the database any time without any code change.
Python Also Support various databases .you can see
support database to python.
- Now today I will tell database handling with the SQLite database.
- SQLite database is light weighted and it can easily embed with embedded applications.
- Now we are going to implement a database using python.
- SQLite library are already in-built with python.
- Now open Command Prompt and open python terminal for handling database operations.
- Now we create a database using the below code when we write in python terminal.
- import sqlite3
- conn = sqlite3.connect('article.db')
- c=conn.cursor()
- conn.close
The above code creates a database whose name is article.db in that directory in which python terminal opens.
Cursor() method is used to do any operations on the database.
- Now we create a table in article.db database as below code:
- import sqlite3
- conn = sqlite3.connect('article.db')
- c=conn.cursor()
- c.execute("create table example(Software VARCHAR, Version Real,Price Real )")
In the above code, we execute create command so you can now understand the use of a cursor in python. We create an example table with three attributes.
- Now we inserting the data into an example table as below.
- c.execute("Insert into example Values('Python',3.4,'100')")
- c.execute("Insert into example Values('Adobe',10.2,'1000')")
- c.execute("Insert into example Values('Office',16,'1000')")
In last we commit the connection to make proper connect between python to a database.
- Now we read the data from databse using code:
- sql = "select * from example"
- for row in c.execute(sql):
- print("Software: "+row[0]
- print("Version: "+str(row[1])
- print("Price: "+str(row[2]))
Output:Software: Python
Version: 3.4
Price: 100.0
Software: Adobe
Version: 10.2
Price: 1000.0
Software: Office
Version: 16.0
Price: 1000.0
- Now we Insert data dynamically in a database using the make method in python:
- def dynamic_data():
- soft = input("Write Software Name : ")
- version = input("Write Versio : ")
- Price= input("Write Price")
-
- c.execute("insert into example(Software,Version,Price) values(?,?,?)" ,(soft,version,Price))
- conn.commit()
- dynamic_data()
Output
• Write Software Name: Google Chrome
• Write Version: 46.343
• Write Price100
- Now we take another database handling with update the data in the database,
- sql="update example set Version = 3.5 where Software = 'Python'"
- c.execute(sql)
- sql = "select * from example"
-
- for row in c.execute(sql):
- print(row)
Output
('Python', 3.5, 100.0)
('Adobe', 10.2, 1000.0)
('Office', 16.0, 1000.0)
('Google Chrome', 46.343, 100.0)
- Another Example of perform detele operation on database:
- sql="delete from example where Software = 'Python'"
- c.execute(sql)
- sql = "select * from example"
- for row in c.execute(sql):
- print(row)
Output:
('Adobe', 10.2, 1000.0)
('Office', 16.0, 1000.0)
('Google Chrome', 46.343, 100.0)
It deletes the python row entry.
So in this article, you will have learned Python handling with the SQLite database.
We used methods that are used in SQLite that also work the same as other databases like MySql, MSSQL.