Mind Blowing menu driven program using SQLite in python

Mistakes you Should not do for SQLite in python. Carefully read or write following codes, not make a mistake. Mind Blowing menu driven program using SQLite in python is written below.

Q1) Write a Python program to create a table and insert some records in that table.
Finally selects all rows from the table and display the records.

import sqlite3 as db
conn=db.connect("record")
curr=conn.cursor()
qry='''create table if not exists friends_record
(id int primary key,name varchar(30),phn_no int,addr varchar(50))'''
curr.execute(qry)
n=int(input("Enter How Many Record You Enter???? "))
for i in range(n):
    id =int(input("Enter Id:"))
    name = (input("Enter Name: "))
    phn_no = int(input("Enter Phone Number: "))
    addr = (input("Enter Address: "))
    insert_qry=f"insert into friends_record values({id},'{name}',{phn_no},'{addr}')"
    curr.execute(insert_qry)
    conn.commit()

selectqry="select * from friends_record"
curr.execute(selectqry)
result=curr.fetchall()
for record in result:
     print(record)

Output:

Enter How Many Record You Enter???? 6
Enter Id:13
Enter Name: chillmar
Enter Phone Number: 4978657094
Enter Address: bgp
Enter Id:16
Enter Name: chillmar quotes
Enter Phone Number: 36754459423
Enter Address: bgp
Enter Id:12
Enter Name: chillmar notes
Enter Phone Number: 685478934
Enter Address: dbg
Enter Id:767
Enter Name: sqlite
Enter Phone Number: 44444998545
Enter Address: ghfjlkjsw
Enter Id:9872
Enter Name: chillmar python
Enter Phone Number: 3487309224
Enter Address: chillmar.in

(13, ‘chillmar’, 4978657094, ‘bgp’)

(16, ‘chillmar quotes’, 36754459423, ‘bgp’)

(12, chillmar notes’, 685478934, ‘dbg’)

(123, ‘chillmar’, 990000123, ‘chillmar.com’)

(767, ‘sqlite’, 44444998545, ‘ghfjlkjsw’)

(9872, ‘chillmar python’, 3487309224, ‘chillmar.in’)


Q2) Create a menu driven program to display options to user, options are
1) Add new Record
2) View all Records
3) Update specific record
4) Delete specific record

Define these functionalities and perform these tasks on SQLite database.

import sqlite3 as db
conn=db.connect("record2")
curr=conn.cursor()
qry='''create table if not exists friends_record2
(id int primary key,name varchar(30),phn_no int,addr varchar(50))'''
curr.execute(qry)
check=1
while check:
    c = int(input("Main Menu\n1) Add new Record\n2) View all Records \n3) Update specific record \n4) Delete specific record\n5) Exit\n"))
    if c==1:
        n = int(input("Enter How Many Record You Enter???? "))
        for i in range(n):
            id = int(input("Enter Id:"))
            name = (input("Enter Name: "))
            phn_no = int(input("Enter Phone Number: "))
            addr = (input("Enter Address: "))
            insert_qry = f"insert into friends_record2 values({id},'{name}',{phn_no},'{addr}')"
            curr.execute(insert_qry)
            conn.commit()
    elif c==2:
        selectqry = "select * from friends_record2"
        curr.execute(selectqry)
        result = curr.fetchall()
        for record in result:
            print(record)
    elif c==3:
        id2 = int(input("Enter Id:"))
        addr2 = (input("Enter Address: "))
        update_qry = f"update friends_record2 set addr='{addr2}' where id={id2}"
        curr.execute(update_qry)
        print(curr.rowcount,"record updated")
        conn.commit()
    elif c==4:
        id3 = int(input("Enter Id:"))
        delt_qry = f"delete from friends_record2 where id={id3}"
        curr.execute(delt_qry)
        print(curr.rowcount, "record deleted")
        conn.commit()
    else:
        print("Program Terminated")
        check=0
print("ThanKyoU")

Output:

Main Menu
1) Add new Record
2) View all Records
3) Update specific record
4) Delete specific record
5) Exit
1
Enter How Many Record You Enter???? 1
Enter Id:0001
Enter Name: chillmar
Enter Phone Number: 00000010001
Enter Address: chillmar.in
Main Menu
1) Add new Record
2) View all Records
3) Update specific record
4) Delete specific record
5) Exit
2
(1, ‘chillmar’, 10001, ‘chillmar.in’)
Main Menu
1) Add new Record
2) View all Records
3) Update specific record
4) Delete specific record
5) Exit
1
Enter How Many Record You Enter???? 2
Enter Id:666
Enter Name: python
Enter Phone Number: 668987998
Enter Address: chillmar.com
Enter Id:3333
Enter Name: sqlite
Enter Phone Number: 4446657
Enter Address: [email protected]
Main Menu
1) Add new Record
2) View all Records
3) Update specific record
4) Delete specific record
5) Exit
2
(1, ‘chillmar’, 10001, ‘chillmar.in’)
(666, ‘python’, 668987998, ‘chillmar.com’)
(3333, ‘sqlite’, 4446657, ‘[email protected]’)
Main Menu
1) Add new Record
2) View all Records
3) Update specific record
4) Delete specific record
5) Exit
3
Enter Id:666
Enter Address: system
1 record updated
Main Menu
1) Add new Record
2) View all Records
3) Update specific record
4) Delete specific record
5) Exit
2
(1, ‘chillmar’, 10001, ‘chillmar.in’)
(666, ‘python’, 668987998, ‘system’)
(3333, ‘sqlite’, 4446657, ‘[email protected]’)
Main Menu
1) Add new Record
2) View all Records
3) Update specific record
4) Delete specific record
5) Exit
4
Enter Id:666
1 record deleted
Main Menu
1) Add new Record
2) View all Records
3) Update specific record
4) Delete specific record
5) Exit
2
(1, ‘chillmar’, 10001, ‘chillmar.in’)
(3333, ‘sqlite’, 4446657, ‘[email protected]’)
Main Menu
1) Add new Record
2) View all Records
3) Update specific record
4) Delete specific record
5) Exit
5
Program Terminated
ThanKyoU



I Think You Understand above Topic “Mind Blowing menu driven program using SQLite in python”.
For any query or suggestions feel free to fill comment section and send it.


More Topic of Python:
Python Program of Basic Question’s Part-1
Python Program of Basic Question’s Part-2
Top Python Program of String for beginner
Highly Recommended Python Program of List for beginner
Mind Blowing While Loop program for beginner
Popular Dictionary Program in Python for Beginner


You may also like...

Leave a Reply

Your email address will not be published.