0% found this document useful (0 votes)
8 views38 pages

SDT Lab - Docx-2

The document outlines a series of exercises related to SQLite, including installation, CRUD operations, database management, and the implementation of triggers and joins. Each exercise provides step-by-step instructions and SQL commands to create tables, insert data, and perform various operations. The results confirm successful execution of the tasks, demonstrating the functionality of SQLite in managing databases.

Uploaded by

naveenkumarmg0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views38 pages

SDT Lab - Docx-2

The document outlines a series of exercises related to SQLite, including installation, CRUD operations, database management, and the implementation of triggers and joins. Each exercise provides step-by-step instructions and SQL commands to create tables, insert data, and perform various operations. The results confirm successful execution of the tasks, demonstrating the functionality of SQLite in managing databases.

Uploaded by

naveenkumarmg0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Ex.

No:1

SQLite Installation, working with SQLite

Aim:

​ To understand how to install the SQLite and its working environment.

Algorithm:

●​ Open the browser


●​ Search the SQLite
●​ Click Precompiled
●​ Download the package and extract it
●​ Copy the SQLite file paste into C drive
●​ Set the Environment Variable
●​ Open the CMD window and work with SQLite environment

Steps:

Step 1: Open google chrome and type SQLite3 in search bar.

Step 2: open the link to navigate the official SQLite site. https://www.sqlite.org/
Step 3: Click download and choose -> Precompiled Binaries for Windows->
sqlite-tools-win-x64-3470100.zip(6.09 MiB)

Step 4: click the downloaded file from download folder, then extract the file and rename as
SQLite3
Step 5: Copy the extracted file into C directory

Step 6: Copy the path and paste as new environment variable


Add the C:\SQLite3 as new environment variable.

Step 7: Open command prompt type SQLite3 and use the environment for query execution.
Result

SQLite 3 installed successfully.


Ex. No:2

SQLite Installation, working with SQLite

Aim:

​ To understand how to install the SQLite and its working environment.

Algorithm:

●​ Open the command prompt


●​ Type SQLite3
●​ Create the table with the name and register number
●​ Insert the values to an table
●​ View all the records
●​ Stop the process

Program

Microsoft Windows [Version 10.0.19045.5131]

(c) Microsoft Corporation. All rights reserved.

C:\Users\staff>SQLite3

SQLite version 3.47.1 2024-11-25 12:07:48

Enter ".help" for usage hints.

Connected to a transient in-memory database.

Use ".open FILENAME" to reopen on a persistent database.

sqlite> create table student(regno number(10),name char(25), course char(20),college


char(10),address varchar(25));

sqlite> insert into student values(11,'AAA','BCA','SRM','Chennai');

sqlite> insert into student values(12,'XXX','BA','SRM','Madurai');


sqlite> insert into student values(13,'YYY','BSC','SRM','Trichy');

sqlite> insert into student values(14,'ZZZ','BSC','SRM','chennai');

sqlite> insert into student values(15,'LLL','MCA','SA','chennai');

sqlite> select * from student;

11|AAA|BCA|SRM|Chennai

12|XXX|BA|SRM|Madurai

13|YYY|BSC|SRM|Trichy

14|ZZZ|BSC|SRM|chennai

15|LLL|MCA|SA|Chennai

Result:

​ The table has been created and values are inserted successfully
Ex.No.3

Attach and Detach database

Aim:

​ To create the simple query to create , attach and detach database.

Algorithm

●​ Open the command window


●​ Type sqlite3 to environment settings
●​ create the database student
●​ Create the table academic with the attributes of name and age
●​ Create the table personal with the attributes of student name and address
●​ Create the attach database with alias name of stu_aca and stu_per
●​ Display the records with whose age is greater than 15 from academic table
●​ Detach the database from academic table
●​ Display the records with who are from chennai using personal table
●​ detach the database from personal table
●​ stop the process

Program:

(i) create database

C:\Users\student>sqlite3 aa.db

SQLite version 3.47.1 2024-11-25 12:07:48

Enter ".help" for usage hints.

(ii) show databases

sqlite> .databases

main: C:\Users\student\aa.db r/w


(iii) attach database

sqlite> attach database 'aa.db' as 'test';

sqlite> .databases

main: C:\Users\student\aa.db r/w

test: C:\Users\student\aa.db r/w

sqlite> attach database 'test.db' as 'newdb';

sqlite> .databases

main: C:\Users\student\aa.db r/w

test: C:\Users\student\aa.db r/w

newdb: C:\Users\student\test.db r/w

(iv) detach database

sqlite> detach database 'newdb';

sqlite> .databases

main: C:\Users\student\aa.db r/w

test: C:\Users\student\aa.db r/w

Result:

Thus the database has been created and attach and detach executed successfully.
Ex.No.4

CRUD Operations

Aim:

​ To write a Program to implement CRUD operations .

Algorithm

●​ Open the command window


●​ Type sqlite3 to environment settings
●​ create the database student
●​ Create the table academic with the attributes of name and age
●​ Display the structure of table
●​ Change the attribute name
●​ Add the attribute mark1 and mark2
●​ Insert 5 records using various insert method
●​ Display all records as output
●​ Display the name of the persons
●​ Add new column as total
●​ Add the mark1 and mark2 as total in each record
●​ Display the name and total as student_ name and Tot_mark
●​ Stop the process

Program:

(i) create table

sqlite> create table academic (name char(20), age int);

(ii) view table structure

sqlite> pragma table_info(academic);

0|name|char(20)|0||0

1|age|INT|0||0
(iii) rename column

sqlite> alter table academic rename name to stu_name;

sqlite> pragma table_info(academic);

0|stu_name|char(20)|0||0

1|age|INT|0||0

(iv) add column

sqlite> alter table academic add column mark1 int;

sqlite> alter table academic add column mark2 int;

sqlite> pragma table_info(academic);

0|stu_name|char(20)|0||0

1|age|INT|0||0

2|mark1|INT|0||0

3|mark2|INT|0||0

(v) insert records

sqlite> insert into academic values('ajay',21,90,70);

sqlite> insert into academic values('vijay',20,60,80);

sqlite> insert into academic ('stu_name',age,mark1,mark2) values ('mano',19,72,46);

sqlite> insert into academic ('stu_name',age,mark1,mark2) values ('minu',18,72,25);


sqlite> insert into academic values(?,?,?,?);

sqlite> insert into academic ('stu_name',age,mark1,mark2) values ('minu',18,72,25);

(vi) display all records

sqlite> select * from academic;

ajay|21|90|70

vijay|20|60|80

mano|19|72|46

minu|18|72|25

|||

minu|18|72|25

(vii) display names

sqlite> select stu_name from academic;

ajay

vijay

mano

minu

minu

(viii) add new column

sqlite> alter table academic add column total int;


sqlite> pragma table_info(academic);

0|stu_name|char(20)|0||0

1|age|INT|0||0

2|mark1|INT|0||0

3|mark2|INT|0||0

4|total|INT|0||0

sqlite> select * from academic;

ajay|21|90|70|

vijay|20|60|80|

mano|19|72|46|

minu|18|72|25|

||||

minu|18|72|25|

(ix) update total from marks

sqlite> update academic set total=mark1+mark2;

sqlite> select * from academic;

ajay|21|90|70|160

vijay|20|60|80|140

mano|19|72|46|118

minu|18|72|25|97

||||

minu|18|72|25|97
(x) to display name and total

sqlite> select stu_name as name, total from academic;

ajay|160

vijay|140

mano|118

minu|97

minu|97

Result:

Thus the database has been created and all CRUD operations implemented
successfully.
Ex No.5

SQLite Conditions

Aim:

​ To write a Program to implement SQLite Conditions

Algorithm:

Program:

(i) create table

sqlite> create table employ(name char(30), id varchar2(30),dept char(30), salary real);

(ii) view structure of table

sqlite> pragma table_info(employ) ;

0|name|char(30)|0||0

1|id|varchar2(30)|0||0

2|dept|char(30)|0||0

3|salary|REAL|0||0

(iii) insert values

sqlite> insert into employ values('aaa','11','maintenance',40000);

sqlite> insert into employ values('bbb','12','maintenance',65000);

sqlite> insert into employ values('ccc','13','production',14040);

sqlite> insert into employ values('ddd','14','production',23040);

sqlite> insert into employ values('eee','15','service',12240);


sqlite> insert into employ values('efff','16','service',92240);

(iv) view records

sqlite> select * from employ;

aaa|11|maintenance|40000.0

bbb|12|maintenance|65000.0

ccc|13|production|14040.0

ddd|14|production|23040.0

eee|15|service|12240.0

efff|16|service|92240.0

(v) sum method

sqlite> select sum(salary) from employ;

246560.0

(vi) where clause

sqlite> select * from employ where dept ='maintenance';

aaa|11|maintenance|40000.0

bbb|12|maintenance|65000.0

(vii) and operator

sqlite> select * from employ where dept ='maintenance' and dept='production';


(viii) or operation

sqlite> select * from employ where dept ='maintenance' or dept='production';

aaa|11|maintenance|40000.0

bbb|12|maintenance|65000.0

ccc|13|production|14040.0

ddd|14|production|23040.0

(ix) greater than operator

sqlite> select * from employ where salary >15000 ;

aaa|11|maintenance|40000.0

bbb|12|maintenance|65000.0

ddd|14|production|23040.0

efff|16|service|92240.0

(x) between condition

sqlite> select * from employ where salary between 15000 and 25000;

ddd|14|production|23040.0

(xi) like operation

sqlite> select * from employ where name like '%e';

eee|15|service|12240.0
sqlite> select * from employ;

aaa|11|maintenance|40000.0

bbb|12|maintenance|65000.0

ccc|13|production|14040.0

ddd|14|production|23040.0

eee|15|service|12240.0

efff|16|service|92240.0

sqlite> select * from employ where name like '%e';

eee|15|service|12240.0

sqlite> select * from employ where name like 'e%';

eee|15|service|12240.0

efff|16|service|92240.0

sqlite> select * from employ where name like '_e%';

eee|15|service|12240.0

sqlite> select * from employ where name like 'e_';

Result:

Thus the database has been created and all conditional operations implemented
successfully.
Ex No.6

Date:13.02.2025

Implementation of Triggers

Aim:

​ To write a Program to implement Triggers

Algorithm:

●​ Start the command prompt


●​ set as sqlite3
●​ create the table record with the given attributes
●​ insert the values to given table
●​ create the trigger for the table record after insertion
●​ try to insert the values on table record to follow the trigger query
●​ create another table employ with give attributes
●​ insert the values to given table
●​ create the trigger for the table employ after insertion
●​ try to insert the values on table record to follow the trigger query
●​ stop the process

Program:

sqlite> create table record(empid int not null, record_date text not null);

sqlite> create trigger record_log after insert

...> on employee

...> begin

...> insert into record(empid,record_date)values(new.idno,datetime('now'));

...> end;
sqlite> insert into employee values(1,'Dev',29,'Financier',30000);

sqlite> SELECT name FROM sqlite_master

...> WHERE type = 'trigger' AND tbl_name = 'employee';

record_log1:

sqlite> select * from record_log;

Parse error: no such table: record_log

sqlite> select* from record;

1|2025-02-12 15:38:14

sqlite> select * from employee;

1|Dev|29|Financier|30000.0

Result:

Thus the database has been created and Triggers implemented successfully.
Ex No.7

Date:13.02.2025

Implementation of JOINS

Aim:

​ To write a Program to implement Joins

Algorithm:

●​ Start the command prompt


●​ set the sqlite3
●​ create table student with id and name as attributes
●​ insert 5 records to the table student
●​ create the table academic with id , dept and address as attributes
●​ insert 5 records to the table
●​ apply the joins query with the table student and academic
●​ apply left outer join with the table student and academic
●​ apply cross join with the table student and academic
●​ stop the process

Program

sqlite> create table student(id int, name char(20));

sqlite> insert into student values(1,'xxx');

sqlite> insert into student values(2,'yyy');

sqlite> insert into student values(3,'zzz');

sqlite> insert into student values(4,'aaa');

sqlite> insert into student values(5,'bbb');

sqlite> select * from student;

1|xxx
2|yyy

3|zzz

4|aaa

5|bbb

sqlite> create table academic(id int,dept char(20),address char(20));

sqlite> insert into academic values(1,'bca','chennai');

sqlite> insert into academic values(2,'bca','chennai');

sqlite> insert into academic values(3,'bca','chennai');

sqlite> insert into academic values(8,'bca','chennai');

sqlite> insert into academic values(9,'bca','chennai');

sqlite> select * from academic;

1|bca|chennai

2|bca|chennai

3|bca|chennai

8|bca|chennai

9|bca|chennai

sqlite> select student.id,academic.dept from student inner join academic on


student.id=academic.id;

1|bca

2|bca

3|bca

sqlite> select student.id,academic.dept from student left outer join academic on


student.id=academic.id;
1|bca

2|bca

3|bca

4|

5|

sqlite> select * from student cross join academic;

1|xxx|1|bca|chennai

1|xxx|2|bca|chennai

1|xxx|3|bca|chennai

1|xxx|8|bca|chennai

1|xxx|9|bca|chennai

2|yyy|1|bca|chennai

2|yyy|2|bca|chennai

2|yyy|3|bca|chennai

2|yyy|8|bca|chennai

2|yyy|9|bca|chennai

3|zzz|1|bca|chennai

3|zzz|2|bca|chennai

3|zzz|3|bca|chennai

3|zzz|8|bca|chennai

3|zzz|9|bca|chennai

4|aaa|1|bca|chennai

4|aaa|2|bca|chennai
4|aaa|3|bca|chennai

4|aaa|8|bca|chennai

4|aaa|9|bca|chennai

5|bbb|1|bca|chennai

5|bbb|2|bca|chennai

5|bbb|3|bca|chennai

5|bbb|8|bca|chennai

5|bbb|9|bca|chennai

Result:

Thus the database has been created and various join queries implemented
successfully.
Ex. No:8

20.02.2025

Simple Python Program

Aim:

​ To create and execute a simple python program

Algorithm

●​ Open the python 3.4.8


●​ Start the program
●​ Get the input of name and register number from the user using input() method
●​ Get the input of mark1 and mark2 from the user
●​ Calculate the total from mark1 and mark2
●​ print the name, register number, mark1,mark2 and total as output
●​ Stop the process

Program

>>> regno=input();

RA22312410200241

>>> name=input("enter the name");

enter the name vino

>>> mark1=int(input("enter mark1"));

enter mark150

>>> mark2=int(input("enter mark2"));

enter mark260

>>> total=mark1+mark2;

>>> print("regno= :",regno);

regno= : RA22312410200241
>>> print("name= ",name);

name= vino

>>> print("m1 = ",mark1,"m2 = ",mark2," total = ",total);

m1 = 50 m2 = 60 total = 110

Result:

Thus the simple python program has been executed successfully.


Ex. No:9

20.02.2025

SQLite Database connectivity from a python program

Aim:

​ To create Program to perform SQLite Database connectivity from a python program

Algorithm

●​ Open the Python 3.5.4 command prompt


●​ Start the program
●​ import the sqlite3 in python window
●​ To open the database using connect object
●​ To execute the create table query with connection object
●​ To execute the query to insert the values to an table with connection object
●​ To execute the select query with connection object to fetch the information from the
table
●​ Print the data from the table to python using for loop
●​ Close the connection
●​ Stop the program

Program

>>> import sqlite3

>>> conn=sqlite3.connect("db1.db");

>>> cur=conn.execute("create table stu1(id int, name char(20))");

>>> cc=conn.execute("insert into stu1 values(1,'xxx')");

>>> cc=conn.execute("insert into stu1 values(2,'yyy')");

>>> v=conn.execute("select * from stu1");


>>> for row in v:

... print(" id is =",row[0]);

... print("name =",row[1]);

id is = 1

name = xxx

id is = 2

name = yyy

>>> conn.close()

Result:

Thus the Program to perform SQLite Database connectivity from a python program
has been executed successfully.
Ex. No:10

27.02.2025

SQLite Database connectivity from a python program

Aim:

​ To create Program to perform SQLite Database connectivity from a python program


for employ database connectivity.

Algorithm

●​ Open the SQLite command prompt to create the database


●​ Open the Python 3.5.4 command prompt
●​ Start the program
●​ import the sqlite3 in python window
●​ To open the database using connect object
●​ To execute the create table query with connection object
●​ To execute the query to insert the values to an table with connection object
●​ To execute the select query with connection object to fetch the information from the
table
●​ Print the data from the table to python using for loop
●​ Close the connection
●​ Stop the program

Program

Microsoft Windows [Version 10.0.19045.5487]

(c) Microsoft Corporation. All rights reserved.

C:\Users\student>sqlite3 ss.db

SQLite version 3.47.2 2024-12-07 20:39:59

Enter ".help" for usage hints.


sqlite> .databases

main: C:\Users\student\ss.db r/w

sqlite>

Python

>>> import sqlite3

>>> conn=sqlite3.connect("c:/users/student/sa.db")

>>> cr=conn.execute("create table emp(id int, ename char(20), age int,salary int)")

>>> cr=conn.execute("insert into emp values(1,'sai',21,20000)")

>>> vr=conn.execute("select * from emp")

>>> for a in vr:

... print("id is ",a[0])

... print(" name is ",a[1])

... print(" salary is ",a[3])

...

id is 1

name is sai

salary is 20000

>>> ca=conn.execute("alter table emp add column pf int")

Result:

Thus the Program to perform SQLite Database connectivity from a python program
has been executed successfully.
Ex. No:11

27.02.2025

SQLite Database connectivity from a python program

Aim:

​ To create Program to perform SQLite Database connectivity from a python program


for employ database connectivity with dynamic inputs.

Algorithm

●​ · Start the process


●​ · Open sqlite3 create emp database
●​ · Open python
●​ · Import sqlite3
●​ · Then make a connection with emp database
●​ · Execute the query to create the table emp_details with the attributes of emp_id,
e_name,salary
●​ · Read the inputs from python using input() method
●​ · Insert these readed inputs to table emp_details
●​ · Insert three records
●​ · Fetch the records from sqlite using select query
●​ · Display as output
●​ · Stop the process

Program

Sqlite3

Microsoft Windows [Version 10.0.19045.5487]

(c) Microsoft Corporation. All rights reserved.

C:\Users\student>sqlite3 ss.db

SQLite version 3.47.1 2024-11-25 12:07:48

Enter ".help" for usage hints.


sqlite> .databases

main: C:\Users\student\ss.db r/w

sqlite>

python

>>> import sqlite3

>>> con=sqlite3.connect("c:/users/student/s.db")

>>> cr=con.execute("create table emp(id int,ename char(20),salary int)")

>>> a=input("enter id")

enter id15

>>> b=input("enter name")

enter namexxxx

>>> c=int(input("enter salary"))

enter salary25000

>>> print(a)

15

>>> print(b)

xxxx

>>> print(c)

25000

>>> se=con.execute("insert into emp values(?,?,?)",(a,b,c))

>>> se=con.execute("insert into emp values(?,?,?)",(a,b,c))

>>> se=con.execute("insert into emp values(?,?,?)",(a,b,c))


>>> sr=con.execute("select * from emp")

>>> for aa in sr:

... print(aa[0])

... print(aa[1])

... print(aa[2])

...

15

xxxx

25000

15

xxxx

25000

15

xxxx

25000

Result:

Thus the Program to perform SQLite Database connectivity from a python program
has been executed successfully.
Ex. No:12

27.02.2025

SQLite Index

Aim:

​ To create SQLite Database to implement the sqlite index concept

Algorithm

●​ Start the process


●​ Open sqlite3 create emp database
●​ Open python
●​ Create the table employ with the attribute of emp id and name
●​ insert the 5 records to the employ table
●​ set the index number for the employ whose salary is above 20000
●​ display the output
●​ Stop the process

Program

sqlite> create table emp(id int, ename char(20), salary int);

sqlite> insert into emp values(1,'xxx',15000);

sqlite> insert into emp values(2,'yyy',15000);

sqlite> insert into emp values(3,'zzz',55000);

sqlite> insert into emp values(4,'aaa',45000);

sqlite> insert into emp values(5,'ccc',5000);

sqlite> select * from emp;

1|xxx|15000

2|yyy|15000

3|zzz|55000
4|aaa|45000

5|ccc|5000

sqlite> create index sal_id on emp(id,salary) where salary>20000;

sqlite> pragma index_list('emp');

0|sal_id|0|c|1

sqlite> pragma index_info('sal_id');

0|0|id

1|2|salary

Result:

Thus the Program to perform SQLite Database has been executed successfully.
Ex. No:13

27.02.2025

SQLite Transaction

Aim:

​ To create SQLite Database to implement the sqlite transaction concept

Algorithm

●​ Start the process


●​ Open sqlite3 create emp database
●​ Open python
●​ Create the table employ with the attribute of emp id and name
●​ set the transaction for the employ table
●​ display the output
●​ Stop the process

Program

sqlite> create table emp(id int, ename char(20), salary int);

sqlite> insert into emp values(1,'xxx',15000);

sqlite> insert into emp values(2,'yyy',15000);

sqlite> insert into emp values(3,'zzz',55000);

sqlite> insert into emp values(4,'aaa',45000);

sqlite> insert into emp values(5,'ccc',5000);

sqlite> select * from emp;

1|xxx|15000

2|yyy|15000

3|zzz|55000
4|aaa|45000

5|ccc|5000

sqlite> insert into emp values(5,'ccc,5000);

' ...> '

(x1...> ;

Parse error: near ";": syntax error

insert into emp values(5,'ccc,5000); ' ;

error here ---^

sqlite> begin transaction;

sqlite> insert into emp values(5,'ccc,5000);

' ...> ';

Parse error: near ";": syntax error

insert into emp values(5,'ccc,5000); ';

error here ---^

sqlite> rollback;

sqlite> select * from emp;

1|xxx|15000

2|yyy|15000

3|zzz|55000

4|aaa|45000

5|ccc|5000

sqlite> begin transaction;


sqlite> insert into emp values(5,'ccc',5000);

sqlite> rollback;

sqlite> select * from emp;

1|xxx|15000

2|yyy|15000

3|zzz|55000

4|aaa|45000

5|ccc|5000

sqlite> begin transaction;

sqlite> insert into emp values(5,'ccc',5000);

sqlite> commit;

sqlite> select * from emp;

1|xxx|15000

2|yyy|15000

3|zzz|55000

4|aaa|45000

5|ccc|5000

5|ccc|5000

Result:

Thus the Program to perform SQLite Database has been executed successfully.

You might also like