Tuesday, 22 October 2024

DB Connection and Create API Using Python for Oracle Database 11g

 import cx_Oracle
from fastapi import FastAPI, Request, HTTPException
import uvicorn
connection = cx_Oracle.connect(user="traders", password="client", dsn="202.142.181.6:1521/orcl")
#connection = conn.connection
app = FastAPI()
cur = connection.cursor()

#################### API's Routes #################
@app.get("/")
def home():
    return "Hello! I'm Alive."
@app.get("/users")
def user():
    cur.execute("select user_login, user_password,user_id from sgi_user")
    result = cur.fetchall()
    return result
@app.post("/login")
async def login(request: Request):
    data = await  request.json()
    user_login = data.get("user_login")
    user_password = data.get("user_password")
    cur.execute("select user_login, user_password from sgi_user where user_login = :user_login and user_password = :user_password", {
        'user_login' : user_login ,
        'user_password' : user_password
    })
    result = cur.fetchone()
    if  result and result[1] == user_password and result[0] == user_login :
        return "Logged In"
    else:
        raise HTTPException(status_code=401, detail="Invalid credentials")
if __name__ == "__main__":
    uvicorn.run("api:app", host='0.0.0.0', port=5000, reload = True)

No comments:

Post a Comment

Check Internet Connectivity on Local System

if (!navigator.onLine) {     showOfflineAlert(); } else {     // Set a strict 3-second network timeout threshold     const controller = new ...