Investigatory Project (Contact Book)
Investigatory Project (Contact Book)
JHUNJHUNU
ABSTRACT
The Project is for Record Keeping
Package
for student. It includes Admission,
Examination, Fees, Library and CCA
modules.
STUDENT NAME
Abhishek
CLASS
12th Science
SUBJECT
Computer Science
TEACHER’S NAME
Mr. Pradeep Swami sir
Python
Introduction
pg. 1
What is Python?
It is used for:
Why Python?
Python Install
To check if you have python installed on a Windows PC, search in the start bar
for Python or run the following on the Command Line (cmd.exe):
C:\Users\Your Name>python –version
pg. 2
Python Comments
Comments can be used to explain Python code.
Comments can be used to make the code more readable.
Comments can be used to prevent execution when testing code.
Creating a Comment
#This is a comment
print("Hello,World!")
Python does not really have a syntax for multi line comments.
Since Python will ignore string literals that are not assigned to a variable, you
can add a multiline string (triple quotes) in your code, and place your comment
inside it:
"""
This is a comment
written in
more than just one line
"""
Variable Names
A variable can have a short name (like x and y) or a more descriptive name
(age, carname, total_volume). Rules for Python variables:
Global Variables
Variables that are created outside of a function (as in all of the examples above)
are known as global variables.
pg. 3
Global variables can be used by everyone, both inside of functions and outside.
x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()
Variables can store data of different types, and different types can do different
things.
Python has the following data types built-in by default, in these categories:
Text Type: str
Python Strings
a = "Hello"
print(a)
Multiline Strings
pg. 4
Python Operators
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
Python Collections
There are four collection data types in the Python programming language:
• List is a collection which is ordered and changeable. Allows duplicate
members.
• Tuple is a collection which is ordered and unchangeable. Allows
duplicate members.
• Set is a collection which is unordered and unindexed. No duplicate
members.
• Dictionary is a collection which is unordered, changeable and indexed.
No duplicate members.
Lists
pg. 5
Method Description
extend() Add the elements of a list (or any iterable), to the end of the current
list
index() Returns the index of the first element with the specified value
What is PIP?
pg. 6
Python Try Except
The try block lets you test a block of code for errors.
The finally block lets you execute code, regardless of the result of the try- and
except blocks.
try:
print(x)
except:
print("An exception occurred")
finally:
print("The 'try except' is finished")
pg. 7
MySQL Database
&
Python
Connectivity
8|Page
Install MySQL Driver
Navigate your command line to the location of PIP, and type the following:
Create Connection
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
passwd="yourpassword"
)
print(mydb)
Creating a Table
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
passwd="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
9|Page
mycursor.execute("CREATE TABLE customers (name VARCHAR(255), address
VARCHAR(255))")
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
passwd="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute(sql, val)
mydb.commit()
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
passwd="yourpassword",
database="mydatabase"
)
10 | P a g e
mycursor = mydb.cursor()
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Update Table
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
passwd="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "UPDATE customers SET address = 'Canyon 123' WHERE address = 'Valley
345'"
mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount,"record(s) affected")
11 | P a g e
CODING
# Contact_Book of mobile using Python and MySQL connectivity
# Made by Abhishek
import mysql.connector as cd
mydb=cd.connect(host="localhost",user="root",passwd="tiger",database="contact
")
mycursor=mydb.cursor()
# ADD Function:------------------------------------------------------------------------------
def add_contact():
L=[]
L.append(mobile_no)
L.append(name)
L.append(address)
12 | P a g e
L.append(email)
cont=(L)
mycursor.execute(sql,cont)
mydb.commit()
13 | P a g e
#Search Function:------------------------------------------------------------------------
def view_contact():
print("1. Mobile_no")
print("2. Name")
print("3. Address")
print("4. email")
if (ch==1):
rl=(s,)
mycursor.execute(sql,rl)
elif (ch==2):
rl=(nm,)
mycursor.execute(sql)
elif (ch==3):
rl=(s,)
14 | P a g e
sql="select * from contact_book where address=%s;"
mycursor.execute(sql,rl)
elif (ch==4):
rl=(s,)
mycursor.execute(sql,rl)
elif (ch==5):
mycursor.execute(sql)
res=mycursor.fetchall()
for x in res:
print(x)
15 | P a g e
#DELETE Function:--------------------------------------------------------------------------
def del_contact():
rl=(name,)
mycursor.execute(sql,rl)
mydb.commit()
16 | P a g e
#MAIN Function:-----------------------------------------------------------------------------
def Main_Menu():
try:
except ValueError:
else:
print("\n")
if(userInput==1):
add_contact()
elif (userInput==2):
view_contact()
elif (userInput==3):
search_contact()
elif (userInput==4):
del_contact()
else:
17 | P a g e
Main_Menu()
Main_Menu
Main_Menu()
18 | P a g e
OUTPUT
SCREENSHOTS
19 | P a g e
Searching
20 | P a g e
Adding a record
21 | P a g e
All record Search
22 | P a g e
Description of Table in MySQL
23 | P a g e
References
• Sumita Arora Book
• Preeti Arora Book
• Internet Credits
www.w3schools.com
www.ictswami.wordpress.com
(Signature of student)
Name of Student
Abhishek
24 | P a g e