how can i add database here in my code using python only

56 Views Asked by At

i am trying to add database to my school project using python only and i am having hard time because the sqlalchemy is not working in Visual Studio Code, it says "Exception has occurred ModuleNotFoundError No module named 'sqlalchemy'", Can someone help me with this one, i cant find the solution and i know this is basic for you guys but not for me since i really hate this language even tho its the best one for me

import sqlalchemy as sa
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

engine = sa.create_engine('postgresql://localhost:5432/ticketing_system')

session = sa.orm.Session(bind=engine)

class Ticket(sa.orm.Base):
    __tablename__ = 'tickets'

    id = sa.Column(sa.Integer, primary_key=True)
    name = sa.Column(sa.String(255))
    age = sa.Column(sa.Integer)
    category = sa.Column(sa.String(255))
    movie = sa.Column(sa.String(255))
    email = sa.Column(sa.String(255))

def book_ticket(name, age, category, movie, email):
    ticket = Ticket(
        name=name,
        age=age,
        category=category,
        movie=movie,
        email=email
    )

    session.add(ticket)
    
    session.commit()

def send_ticket_email(ticket):
  
    msg = MIMEMultipart()

    msg['From'] = '[email protected]'
    msg['To'] = ticket.email
    msg['Subject'] = 'Movie Ticket Receipt'

    body = f'Dear {ticket.name},\n\nYour movie ticket has been booked successfully. Here are the details:\n\nMovie: {ticket.movie}\nCategory: {ticket.category}\nAge: {ticket.age}\n\nPlease enjoy your movie!'

    msg.attach(MIMEText(body, 'plain'))

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login('[email protected]', 'xlhh egta djrm qumm')
    server.send_message(msg)
    server.quit()


def main():
   
    print("\n\tWelcome to CC03 Cinema\nPlease Choose an Operation Below\n\n\t"
          "1. Book a movie ticket\n\t2. Available Movies\n\t3. TAHAHAHAHA\n\t4. EXIT")

   
    choose = int(input("\nChoose >> "))

    
    match choose:

        case 1:
            
            name = str(input("Enter your fullname: "))
            age = int(input("Enter your age: "))
            category = input("Choose Category: ")
            movie = input("Choose Movie: ")

          
            book_ticket(name, age, category, movie, ticket.email)

          
            send_ticket_email(ticket)

        case 2:
            AvailableMovies()

        case 3:
            print("\nTHAHAHAHA")

        case 4:
            exit()

        case default:
            print("\nInvalid Selection")

if __name__ == '__main__':
    main()
1

There are 1 best solutions below

0
On
pip install sqlalchemy

If you're in a virtual environment, select it and activate it then install sqlalchemy.