Python Database Handling


In this chapter, you will learn to use database with python.


A database is an organized collection of data, generally stored and accessed electronically from a computer system. Where databases are more complex they are often developed using formal design and modeling techniques.
Python language supports many database servers as:
  • MySQL
  • PostgreSQL
  • Microsoft SQL Server
  • Oracle
  • SQLite
We can work with any database without changing code because mostly the Database works with the same method for handling databases that give the flexibility to change the database any time without any code change.
Python Also Support various databases .you can see support database to python.
  1. Now today I will tell database handling with the SQLite database.
  2. SQLite database is light weighted and it can easily embed with embedded applications.
  3. Now we are going to implement a database using python.
  4. SQLite library are already in-built with python.
  5. Now open Command Prompt and open python terminal for handling database operations.
  6. Now we create a database using the below code when we write in python terminal.
    1. import sqlite3  
    2. conn = sqlite3.connect('article.db')  
    3. c=conn.cursor()  
    4. 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.
  7. Now we create a table in article.db database as below code:
    1. import sqlite3  
    2. conn = sqlite3.connect('article.db')  
    3. c=conn.cursor()  
    4. 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.
  8. Now we inserting the data into an example table as below.
    1. c.execute("Insert into example Values('Python',3.4,'100')")  
    2. c.execute("Insert into example Values('Adobe',10.2,'1000')")  
    3. c.execute("Insert into example Values('Office',16,'1000')"
    In last we commit the connection to make proper connect between python to a database.
  9. Now we read the data from databse using code:
    1. sql = "select * from example"  
    2. for row in c.execute(sql):  
    3.     print("Software: "+row[0]  
    4.     print("Version: "+str(row[1])  
    5.     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
  10. Now we Insert data dynamically in a database using the make method in python:
    1. def dynamic_data():   
    2.     soft = input("Write Software Name : ")  
    3.     version = input("Write Versio : ")  
    4.     Price= input("Write Price")  
    6. c.execute("insert into example(Software,Version,Price) values(?,?,?)" ,(soft,version,Price))  
    7. conn.commit()  
    8. dynamic_data() 
    • Write Software Name: Google Chrome
    • Write Version: 46.343
    • Write Price100
  11. Now we take another database handling with update the data in the database,
    1. sql="update example set Version = 3.5 where Software = 'Python'"  
    2. c.execute(sql)  
    3. sql = "select * from example"  
    5. for row in c.execute(sql):  
    6.    print(row) 
    ('Python', 3.5, 100.0)
    ('Adobe', 10.2, 1000.0)
    ('Office', 16.0, 1000.0)
    ('Google Chrome', 46.343, 100.0)
  12. Another Example of perform detele operation on database:
    1. sql="delete from example where Software = 'Python'"  
    2. c.execute(sql)  
    3. sql = "select * from example"  
    4. for row in c.execute(sql):  
    5.     print(row) 
    ('Adobe', 10.2, 1000.0)
    ('Office', 16.0, 1000.0)
    ('Google Chrome', 46.343, 100.0)
    It deletes the python row entry.


From the next chapter onwards will see some implementations of Python into some real-world applications.
Neeraj Kumar
318 8.1k 2.2m
Next » Project 1: Patterns In Python