[Go to site: main page, start]

0% found this document useful (0 votes)
2 views6 pages

Python Stack and MySQL Database Operations

Uploaded by

greekmann007
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)
2 views6 pages

Python Stack and MySQL Database Operations

Uploaded by

greekmann007
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

# Python program of stack using list

def Push(stk, item):


[Link](item)

def Pop(stk):
if len(stk) == 0:
return "Underflow"
else:
return [Link]()

def Display(stk):
if len(stk):
print("Stack is empty.")
else:
for i in range(-1,-(len(Stk)+1),-1):
print(stk[i])

stack = []
ans = True
while ans:
print("\n--- STACK MENU ---")
print("1. PUSH")
print("2. POP")
print("3. DISPLAY")
print("4. EXIT")

choice = int(input("Enter choice: "))

if choice == 1:
val = input("Enter value to push: ")
Push(stack, val)

elif choice == 2:
result = Pop(stack)
if result == "Underflow":
print("Underflow! The stack is empty.")
else:
print("Popped element is:", result)

elif choice == 3:
Display(stack)

elif choice == 4:
print("Exited”)
ans = False

else:
print("Invalid input! Please choose 1-4.")

#To connect to MySQL and create the


school database.

import [Link]

mydb = [Link](
host="localhost",
user="root",
password="Dpskorba1234@")

mycursor = [Link]()
[Link]("CREATE DATABASE IF NOT EXISTS school")

print("Database 'school' created successfully.")


#To create a student table with Roll No,
Name, Class, and Marks.

import [Link]
mydb = [Link](
host="localhost",
user="root",
password="Dpskorba1234@",
database="school")

mycursor = [Link]()
[Link](
"CREATE TABLE IF NOT EXISTS student (roll_no INT PRIMARY
KEY, name VARCHAR(50), class VARCHAR(10), marks INT)"
)
print("Table 'student' created successfully.")

#To insert a single record into the


student table.

import [Link]
mydb = [Link](
host="localhost",
user="root",
password="Dpskorba1234@",
database="school")

mycursor = [Link]()
sql = "INSERT INTO student (roll_no, name, class, marks) VALUES (%s,
%s, %s, %s)"
val = (101, "Amit Kumar", "12 CS", 95)

[Link](sql, val)
[Link]()

print("One record inserted.")


# To fetch and display all records from
the student table.
import [Link]

mydb = [Link](
host="localhost",
user="root",
password="Dpskorba1234@",
database="school"
)

mycursor = [Link]()
[Link]("SELECT * FROM student")

for x in [Link]():
print(x)

#To insert multiple records at once.

import [Link]

mydb = [Link](
host="localhost",
user="root",
password="Dpskorba1234@",
database="school")

mycursor = [Link]()
sql = "INSERT INTO student (roll_no, name, class, marks) VALUES (%s,
%s, %s, %s)"

val = [
(102, "Sita Sharma", "12 CS", 88),
(103, "Rahul Singh", "12 CS", 72),
(104, "Ritesh Sahu", "12 CS", 99)
]

[Link](sql, val)
[Link]()

print([Link], "records inserted.")

#To update marks of a student using roll


number.

import [Link]

mydb = [Link](
host="localhost",
user="root",
password="Dpskorba1234@",
database="school"
)

mycursor = [Link]()
[Link]("UPDATE student SET marks = 100 WHERE roll_no =
101")
[Link]()

print("Record updated.")
#To delete a student record from the
table.

import [Link]

mydb = [Link](
host="localhost",
user="root",
password="Dpskorba1234@",
database="school"
)

mycursor = [Link]()
[Link]("DELETE FROM student WHERE roll_no = 103")
[Link]()

print("Record deleted.")

You might also like