Computer Science Project
Computer Science Project
COMPUTER
SCIENCE PROJECT
SESSION : 2024 - 25
NAME : K.SRINIVASH
CLASS AND SECTION : XII - A
TOPIC : SQL AND PYTHON CONNECTIVITY
TEACHER INCHARGE : AKILA. K
SUB CODE : 083
REGISTRATION NUBER : ___________________________
VANI VIDYALAYA
SENIOR SECONDARY AND JUNIOR COLLEGE
Affiliated to CBSE - New Delhi (Aff.No .1930186)
No.12, VENBULIAMMAN KOIL STREET, WEST K. K. NAGAR, CHENNAI 600 078
CERTIFICATE
Certified to be bonafide record of the work done by
Selvan K. SRINIVASH of XII Std A Section
in the COMPUTER SCIENCE Laboratory during in the
Academic Year 2024 - 25
Registration Number
SNO DESCRIPTION PG NO
1) ACKNOWLEDGEMENT 4
2) INTRODUCTION TO PYTHON 5
10
6) SOURCE CODES
13
7) OUTPUTS
8) BIBLIOGRAPHY 16
3
ACKNOWLEDGEMENT
4
INTRODUCTION TO PYTHON
6
HARDWARE AND SOFTWARE
REQUIREMENTS
IV. RAM:512MB+
7
DATABASE , TABLE & FUNCTIONS USED
Key Features:
1. Open-source and free to use
2. Supports SQL and various programming languages
3. Scalable and high-performance
4. Supports various data types and storage engines
5. Robust security features
FUNCTIONS USED :
PYTHON IN-BUILT :
PRINT() - The print() function in Python is used to output text or values
to the screen. It is a built-in function that can take multiple arguments.
Syntax:
print(object(s), sep=separator, end=endvalue, file=fileobject)
CURSOR() - In Python, a cursor is an object that allows you to interact
with a database. It is used to execute SQL queries and retrieve data from
a database.
Key Features:
1. Connect to a database using a library such as sqlite3 or mysql-connector-python.
2. Create a cursor object using the cursor() method.
3. Execute SQL queries using the execute() method.
4. Fetch data using the fetchone(), fetchmany(), or fetchall() methods.
5. Close the cursor and connection when finished.
SQL COMMANDS :
DELETE - statement in SQL is used to delete existing records from a database table.
Syntax:
DELETE FROM table_name WHERE condition;
CREATE - The CREATE statement in SQL is used to create a new database table or
other database objects such as indexes, views, and triggers.
Syntax:
CREATE TABLE table_name ( column1 data_type, column2 data_type, .... );
9
SOURCE CODE
#=====================================================
import mysql.connector as a
p = str(input('ENTER THE PASSWORD FOR YOUR SQL '))
d = str(input('ENTER THE NAME OF THE DATABASE: '))
e = list(input('ENTER THE NAME OF THE TABLE: '))
b = a.connect(host = 'localhost',user = 'root', passwd = p,database = d)
C = b.cursor()
C.execute('select * from emp1 ;')
while True :
data = C.fetchone()
if data != None:
print('|',data[0],'|',data[1],'|',data[2],'|',data[3],'|')
else:
break
#=====================================================
def emp1_insert_rec():
C = b.cursor()
c1 = int(input('ENTER ID FOR COL 1:'))
c2 = str(input('ENTER NAME FOR COL 2:'))
c3 = str(input('ENTER JOB FOR COL 3:'))
c4 = int(input('ENTER SALARY FOR COL 4:'))
C.execute('insert into emp1 values({},"{}","{}",{})'.format(c1,c2,c3,c4))
b.commit()
#=====================================================
def emp1_update_rec():
C = b.cursor()
ch = int(input('ENTER THE FIELD DATA TO BE UPDATED : (job - 1/salary - 2) : '))
if ch == 2:
rol = int(input('ENTER THE ID (PRIMARY KEY):'))
sal = int(input('ENTER SALARY TO BE UPDATED :'))
C.execute('update emp1 set salary = {} where id = {};'.format(sal,rol))
b.commit()
10
elif ch ==1 :
rol = int(input('ENTER THE ID (PRIMARY KEY):'))
job = str(input('ENTER JOB TO BE UPDATED :'))
C.execute('update emp1 set job = "{}" where id = {};'.format(job,rol))
b.commit()
#==========================================================
def emp1_delete_rec():
C = b.cursor()
i = int(input('ENTER THE ID (PRIMARY KEY):'))
C.execute('delete from emp1 where id = {}'.format(i))
b.commit()
#==========================================================
def emp1_view_allrec():
ch = int(input('VIEW ALL RECORD (1) OR SPECIFICE REC (2) : '))
if ch == 1:
C = b.cursor()
C.execute('select * from emp1 ;')
print('-'*50)
while True:
data = C.fetchone()
if data != None:
print('|',data[0],'|',data[1],'|',data[2],'|',data[3],'|')
else:
break
print('-'*50)
elif ch == 2:
C = b.cursor()
i = int(input(' ENTER THE ID OF THE REC TO BE DISPLAYED : '))
C.execute('select * from emp1 where id= {}'.format(i))
data = C.fetchall()
print(data)
else :
print('wrong choice type again')
#==========================================================
11
#==========================================================
while True:
ch=int(input('MENU,\n1) INSERT,\n2) UPDATE,\n3) DELETE,\n4) VIEW RECORD,\n5) EXIT,\n(1/2/3/4/5) : '))
print('\n')
if ch == 1:
a = int(input('ENTER THE NO.OF ROWS TO BE INSERTED:'))
print('\n')
for i in range(0,a):
emp1_insert_rec()
print('\n')
elif ch == 2:
emp1_update_rec()
print('\n')
elif ch == 3:
emp1_delete_rec()
print('\n')
elif ch == 4:
emp1_view_allrec()
print('\n')
else:
break
#==========================================================
12
OUTPUTS
INSERTE
UPDATE
13
DELETE
VIEW
14
TABLE CREATED IN MY SQL :
15
BIBLIOGRAPHY
16
THANK
YOU
17