Flask Programming

Hands On No. 8 : Working With SQLAlchemy Database

Resources

Source Code of hello.py
from sqlalchemy import create_engine
from sqlalchemy import text
engine = create_engine("sqlite:///dm.db", echo=True)

c = int(input("Enter Your Choice : \n1 :Insert \n2: Delete \n3: Update \n4 :View\n"))
print(c)
    
if(c == 1):
    print("Performing Insert Operation")
    xx = int(input("Enter No "))
    yy = input("Enter Name ")
    with engine.connect() as conn:
        conn.execute(text("INSERT INTO emp (x, y) VALUES (" + str(xx) + ",'" + yy + "')"))
        conn.commit()

elif(c == 2):
    print("Performing Delete Operation")
    xx = int(input("Enter No "))
    with engine.connect() as conn:
        conn.execute(text("delete from emp where x=" + str(xx) +""))
        conn.commit()

elif(c == 3):
    print("Performing Update Operation")
    xx = int(input("Enter No "))
    yy = input("Enter Name ")
    with engine.connect() as conn:
        conn.execute(text("UPDATE emp SET y='" + yy + "' where x=" + str(xx) + ""))
        conn.commit()

elif(c == 4):
    print("Performing View Operation")
    with engine.connect() as conn:
        result = conn.execute(text("SELECT count(*) FROM emp"))
        n=0
        for row in result:
            n = row[0]
            if(n == 0):
                print("No Data Found")
            else:
                print(""+ str(n) + " Data Found")
                result1 = conn.execute(text("SELECT x,y FROM emp"))
                for row1 in result1:
                    print(f"Student No. : {row1.x} Name : {row1.y}")
else:
    print("Wrong Choice")