0% found this document useful (0 votes)
19 views3 pages

ESTATE

Uploaded by

jainkavya10756
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

ESTATE

Uploaded by

jainkavya10756
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

CREATE DATABASE estate_management;

Create a table to store estate details:


sql
Copy code
USE estate_management;

CREATE TABLE estates (


id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
address VARCHAR(255) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
size DECIMAL(10, 2) NOT NULL,
type VARCHAR(50) NOT NULL);.

pip install mysql-connector-python tabulate


import mysql.connector
from tabulate import tabulate

# Database connection
def connect_to_database():
return mysql.connector.connect(
host="localhost", # Replace with your MySQL host (e.g.,
'localhost')
user="root", # Replace with your MySQL username
password="password", # Replace with your MySQL password
database="estate_management"
)

# Create new estate entry


def create_estate(name, address, price, size, estate_type):
connection = connect_to_database()
cursor = connection.cursor()

query = "INSERT INTO estates (name, address, price, size, type) VALUES
(%s, %s, %s, %s, %s)"
cursor.execute(query, (name, address, price, size, estate_type))

connection.commit()
print(f"Estate '{name}' added successfully!")
cursor.close()
connection.close()

# Read all estates from the database


def read_estates():
connection = connect_to_database()
cursor = connection.cursor()

query = "SELECT * FROM estates"


cursor.execute(query)

estates = cursor.fetchall()
cursor.close()
connection.close()

return estates

# Update an estate entry


def update_estate(estate_id, name=None, address=None, price=None,
size=None, estate_type=None):
connection = connect_to_database()
cursor = connection.cursor()
update_query = "UPDATE estates SET"
params = []

if name:
update_query += " name = %s,"
params.append(name)
if address:
update_query += " address = %s,"
params.append(address)
if price:
update_query += " price = %s,"
params.append(price)
if size:
update_query += " size = %s,"
params.append(size)
if estate_type:
update_query += " type = %s,"
params.append(estate_type)

update_query = update_query.rstrip(",") # Remove trailing comma


update_query += " WHERE id = %s"
params.append(estate_id)

cursor.execute(update_query, tuple(params))
connection.commit()
print(f"Estate with ID {estate_id} updated successfully!")
cursor.close()
connection.close()

# Delete an estate entry


def delete_estate(estate_id):
connection = connect_to_database()
cursor = connection.cursor()

query = "DELETE FROM estates WHERE id = %s"


cursor.execute(query, (estate_id,))

connection.commit()
print(f"Estate with ID {estate_id} deleted successfully!")
cursor.close()
connection.close()

# Display estates in tabular form


def display_estates():
estates = read_estates()
headers = ["ID", "Name", "Address", "Price", "Size", "Type"]
print(tabulate(estates, headers=headers, tablefmt="grid"))

# Main program
if __name__ == "__main__":
# Example: Adding some estates
create_estate("Luxury Villa", "123 Sunset Blvd", 1200000.00, 450.00,
"Villa")
create_estate("City Apartment", "456 Main St", 500000.00, 85.00,
"Apartment")

# Display estates
display_estates()

# Example: Updating an estate


update_estate(1, price=1250000.00)

# Display updated estates


print("\nUpdated Estate List:")
display_estates()

# Example: Deleting an estate


delete_estate(2)

# Display estates after deletion


print("\nEstate List After Deletion:")
display_estates()

Output:
+----+----------------+-------------------+-----------+-------+------------
+
| ID | Name | Address | Price | Size | Type
|
+----+----------------+-------------------+-----------+-------+------------
+
| 1 | Luxury Villa | 123 Sunset Blvd | 1200000.0 | 450.0 | Villa
|
| 2 | City Apartment | 456 Main St | 500000.0 | 85.0 | Apartment
|
+----+----------------+-------------------+-----------+-------+------------
+

Estate with ID 1 updated successfully!

Updated Estate List:


+----+----------------+-------------------+-----------+-------+------------
+
| ID | Name | Address | Price | Size | Type
|
+----+----------------+-------------------+-----------+-------+------------
+
| 1 | Luxury Villa | 123 Sunset Blvd | 1250000.0 | 450.0 | Villa
|
| 2 | City Apartment | 456 Main St | 500000.0 | 85.0 | Apartment
|
+----+----------------+-------------------+-----------+-------+------------
+

Estate with ID 2 deleted successfully!

Estate List After Deletion:


+----+----------------+-------------------+-----------+-------+------------
+
| ID | Name | Address | Price | Size | Type
|
+----+----------------+-------------------+-----------+-------+------------
+
| 1 | Luxury Villa | 123 Sunset Blvd | 1250000.0 | 450.0 | Villa
|

You might also like