Dbms Code & Viva
Dbms Code & Viva
-- 1. Create Database
CREATE DATABASE IF NOT EXISTS lab_views_demo;
-- 2. Use Database
USE lab_views_demo;
EXP: 13
EXP: 14
EXP:15
SQL CODE
import tkinter as tk
from tkinter import messagebox
import mysql.connector
# Connect to MySQL
conn = mysql.connector.connect(
host='localhost',
user='root',
password='', # Replace with your MySQL password
database='studentdb'
)
cursor = conn.cursor()
# GUI setup
root = tk.Tk()
root.title("Simple GUI Database App")
id_entry = tk.Entry(root)
title_entry = tk.Entry(root)
author_entry = tk.Entry(root)
price_entry = tk.Entry(root)
id_entry.grid(row=0, column=1)
title_entry.grid(row=1, column=1)
author_entry.grid(row=2, column=1)
price_entry.grid(row=3, column=1)
# Insert Button
insert_btn = tk.Button(root, text="Insert Book",
command=insert_data)
insert_btn.grid(row=4, column=0, columnspan=2)
root.mainloop()
cursor.close()
conn.close()
5. Advantages?
Easy interaction, less coding for users, error handling visually.
EXP:16
USE property_db;
PYTHON
import mysql.connector
# Connect to MySQL
con = mysql.connector.connect(
host="localhost",
user="root",
password="", database="property_db"
)
while True:
print("\n--- Property Management Menu ---")
print("1. Insert Shop Details")
print("2. Update Shop")
print("3. Delete Shop")
print("4. Display All Shops")
print("5. Exit")
if ch == 1:
n = int(input("Enter number of shops to add: "))
for _ in range(n):
shop_id = int(input("Enter Shop ID: "))
name = input("Enter Shop Name: ")
floor = int(input("Enter Floor: "))
area = float(input("Enter Area (in sq.ft): "))
elif ch == 2:
shop_id = int(input("Enter Shop ID to update: "))
name = input("Enter New Shop Name: ")
floor = int(input("Enter New Floor: "))
area = float(input("Enter New Area: "))
elif ch == 3:
name = input("Enter Shop Name to delete: ")
query = "DELETE FROM shops WHERE shop_name=%s"
cursor.execute(query, (name,))
🗑️ Shop deleted successfully!")
con.commit()
print("
elif ch == 4:
cursor.execute("SELECT * FROM shops")
📋 Shop Records:")
records = cursor.fetchall()
print("\n
for row in records:
print(f"ID: {row[0]}, Name: {row[1]}, Floor:
{row[2]}, Area: {row[3]} sq.ft")
EXP:17
SQL CODE:
CREATE DATABASE IF NOT EXISTS student_db;
USE student_db;
CREATE TABLE IF NOT EXISTS students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
roll_no VARCHAR(20),
department VARCHAR(100),
email VARCHAR(100)
);
SHOW CREATE TABLE students;
PYTHON
import mysql.connector
from flask import Flask, request, render_template
app = Flask(__name__)
try:
# Connect to MySQL
conn = mysql.connector.connect(
host='localhost',
user='root',
password='', # <-- Replace here
database='student_db' # <-- Replace here
)
cursor = conn.cursor()
cursor.execute(query, values)
conn.commit()
cursor.close()
conn.close()
if __name__ == '__main__':
app.run(debug=True)
HTML //SUCCESS
<!DOCTYPE html>
<html>
<head>
<title>Success</title>
</head>
<body>
<h2>Registration Successful!</h2>
<p>Thank you, {{ name }}! You have successfully registered
for the {{ department }} course.</p>
</body>
</html>
VIVA:
Connect frontend to backend (e.g., Flask), then use Python to link with MySQL using
mysql.connector.
Relational DBs ensure structured, secure, and scalable data with powerful SQL
queries.
Primary keys uniquely identify records and maintain data accuracy in student
databases.
EXP:18
3. How do you handle customer check-in and check-out in the system?
➤ By storing and updating check_in and check_out dates in the
database.
EXP:18
PYTHON
from flask import Flask, request, render_template_string
import mysql.connector
app = Flask(__name__)
form_html = """
<!doctype html>
<title>Hotel Booking Form</title>
<h2>Hotel Booking</h2>
<form method="POST" action="/">
Name: <input type="text" name="name" required><br><br>
Email: <input type="email" name="email" required><br><br>
Phone: <input type="text" name="phone" required><br><br>
Check-in Date: <input type="date" name="check_in"
required><br><br>
Check-out Date: <input type="date" name="check_out"
required><br><br>
Room Type:
<select name="room_type" required>
<option value="Single">Single</option>
<option value="Double">Double</option>
<option value="Suite">Suite</option>
</select><br><br>
<input type="submit" value="Book Now">
</form>
"""
try:
conn = mysql.connector.connect(**db_config)
cursor = conn.cursor()
sql = """INSERT INTO bookings (name, email, phone,
check_in, check_out, room_type)
VALUES (%s, %s, %s, %s, %s, %s)"""
cursor.execute(sql, tuple(data.values()))
conn.commit()
cursor.close()
conn.close()
return "<h3>Booking Successful! Thank you,
{}</h3>".format(data['name'])
except Exception as e:
return f"<h3>Error: {e}</h3>"
return render_template_string(form_html)
if __name__ == '__main__':
app.run(debug=True)
HTML //FORM
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1" />
<title>Hotel Booking Form</title>
<style>
body {
font-family: Arial, sans-serif;
background: #f4f7f8;
padding: 20px;
}
.booking-form {
background: white;
max-width: 500px;
margin: 0 auto;
padding: 30px;
border-radius: 8px;
box-shadow: 0 0 15px rgba(0,0,0,0.1);
}
.booking-form h2 {
margin-bottom: 20px;
color: #333;
}
.booking-form label {
display: block;
margin-bottom: 6px;
font-weight: bold;
color: #555;
}
.booking-form input[type="text"],
.booking-form input[type="email"],
.booking-form input[type="date"],
.booking-form select,
.booking-form input[type="number"] {
width: 100%;
padding: 10px;
margin-bottom: 16px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
font-size: 16px;
}
.booking-form button {
background: #007bff;
color: white;
padding: 12px;
width: 100%;
border: none;
border-radius: 4px;
font-size: 18px;
cursor: pointer;
transition: background 0.3s ease;
}
.booking-form button:hover {
background: #0056b3;
}
</style>
</head>
<body>
<div class="booking-form">
<h2>Hotel Booking Form</h2>
<form action="/submit_booking" method="POST">
<label for="fullname">Full Name</label>
<input type="text" id="fullname" name="fullname"
required placeholder="Your full name" />
<!DOCTYPE html>
<html>
<head>
<title>Booking Confirmed</title>
</head>
<body>
<h2>Thank you {{ name }}!</h2>
<p>Your booking for a {{ room_type }} room from {{ check_in
}} to {{ check_out }} has been confirmed.</p>
</body>
</html>