Saturday, 19 June 2010

CRUD operations in SQLite

Inserting the emp information in the table:

#import the sqlite module

import sqlite3 as s

#connecting/creation to the database

conn=s.connect('escdb1.db')

#creation table/ if already exists insert the data'

 

conn.execute("insert into APSSDC (id,name,age,address,salary) values(001,'ramesh','23','vij',24000.00)");

conn.execute("insert into APSSDC (id,name,age,address,salary) values(002,'sita','23','ctr',25000.00)");

conn.commit()

print("Stored successfully")

conn.close()

"""

stored successfully in the table

"""

 

#Retrieving the data from the database:

#import the sqlite module

import sqlite3 as s

#connecting/creation to the database

conn=s.connect('escdb1.db')

#creation table/ if already exists insert the data'

 

##conn.execute("insert into APSSDC (id,name,age,address,salary) values(107,'Ravi','26','pksm',28000.00)");

c=conn.execute("SELECT id,name,age,address,salary from APSSDC")

for row in c:

    print("EMP ID=",row[0])

    print("EMP Name=",row[1])

    print("EMP Age=",row[2])

    print("EMP Address=",row[3])

    print("EMP Salary=",row[4],"\n")

print("Retrieved successfully")

##conn.commit()

conn.close()

"""

EMP ID= 1

EMP Name= ramesh

EMP Age= 23

EMP Address= vij

EMP Salary= 24000.0

 

EMP ID= 2

EMP Name= sita

EMP Age= 23

EMP Address= ctr

EMP Salary= 25000.0

 

EMP ID= 107

EMP Name= Ravi

EMP Age= 26

EMP Address= pksm

EMP Salary= 28000.0

 

Retrieved successfully

"""

Updating the records:

#Updating the data in the table

 

#import the sqlite module

import sqlite3 as s

#connecting/creation to the database

conn=s.connect('escdb1.db')

#creation table/ if already exists insert the data'

##conn.execute("insert into APSSDC (id,name,age,address,salary) values(107,'Ravi','26','pksm',28000.00)");

##c=conn.execute("SELECT id,name,age,address,salary from APSSDC")

conn.execute("UPDATE APSSDC set address='Vizianagaram' where ID=1")

conn.commit()

conn.close()

"""

EMP ID= 1

EMP Name= ramesh

EMP Age= 23

EMP Address= vij

EMP Salary= 24000.0

 

EMP ID= 2

EMP Name= sita

EMP Age= 23

EMP Address= ctr

EMP Salary= 25000.0

 

EMP ID= 107

EMP Name= Ravi

EMP Age= 26

EMP Address= pksm

EMP Salary= 28000.0

 

Retrieved successfully

"""

Deletion Operation:

#Deleting operation

 

#import the sqlite module

import sqlite3 as s

#connecting/creation to the database

conn=s.connect('escdb1.db')

 

##c=conn.execute("SELECT id,name,age,address,salary from APSSDC")

conn.execute("DELETE from APSSDC where ID=1")

conn.commit()

c=conn.execute("SELECT id,name,age,address,salary from APSSDC")

for row in c:

    print("EMP ID=",row[0])

    print("EMP Name=",row[1])

    print("EMP Age=",row[2])

    print("EMP Address=",row[3])

    print("EMP Salary=",row[4],"\n")

print("Deleted successfully")

 

conn.close()

"""

EMP ID= 107

EMP Name= Ravi

EMP Age= 26

EMP Address= pksm

EMP Salary= 28000.0

 

Deleted successfully"""

Related Video: 
Next:

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(&qu...