Dastur kodi - Zbekiston respublikasi raqamli texnologiyalari vazirligi muhammad al-xorazmiy nomidagi toshkent

|
Zbekiston respublikasi raqamli texnologiyalari vazirligi muhammad al-xorazmiy nomidagi toshkent
|
bet | 8/11 | Sana | 27.05.2024 | Hajmi | 2,04 Mb. | | #254491 |
Bog'liq IndividualDastur kodi:
from fastapi import Depends, FastAPI, HTTPException
from sqlalchemy.orm import Session
from item_app import crud, models, schemas
from item_app.database import SessionLocal, engine
models.Base.metadata.create_all(bind=engine)
app = FastAPI(title="Sardor API")
# Dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
@app.post("/users/", response_model=schemas.User)
def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
db_user = crud.get_user_by_email(db, email=user.email)
if db_user:
raise HTTPException(status_code=400, detail="Email already registered")
return crud.create_user(db=db, user=user)
@app.get("/users/", response_model=list[schemas.User])
def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
users = crud.get_users(db, skip=skip, limit=limit)
return users
@app.get("/users/{user_id}", response_model=schemas.User)
def read_user(user_id: int, db: Session = Depends(get_db)):
db_user = crud.get_user(db, user_id=user_id)
if db_user is None:
raise HTTPException(status_code=404, detail="User not found")
return db_user
@app.post("/users/{user_id}/items/", response_model=schemas.Item)
def create_item_for_user(
user_id: int, item: schemas.ItemCreate, db: Session = Depends(get_db)
):
return crud.create_user_item(db=db, item=item, user_id=user_id)
@app.get("/items/", response_model=list[schemas.Item])
def read_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
items = crud.get_items(db, skip=skip, limit=limit)
return items
Connection conn = DriverManager.getConnection(
"jdbc:somejdbcvendor:other data needed by some jdbc vendor",
"myLogin",
"myPassword");
try {
/* you use the connection here */
} finally {
//It's important to close the connection when you are done with it
try {
conn.close();
} catch (Throwable e) { /* Propagate the original exception
instead of this one that you want just logged */
logger.warn("Could not close JDBC Connection",e);
}
}
Java SE 7-dan boshlab, yuqoridagi kodni soddalashtirish uchun Java-ning manbalar bilan ishlash bayonotidan foydalanishingiz mumkin.
try (Connection conn = DriverManager.getConnection(
"jdbc:somejdbcvendor:other data needed by some jdbc vendor",
"myLogin",
"myPassword")) {
/* you use the connection here */
} // the VM will take care of closing the connection
Aloqa o'rnatilgandan so'ng, bayonot yaratilishi mumkin.
try (Statement stmt = conn.createStatement()) {
stmt.executeUpdate("INSERT INTO MyTable(name) VALUES ('my name')");
}
Shuni esda tutish kerakki, ulanishlar, bayonotlar va ResultSets ko'pincha operatsion tizim manbalarini, masalan, rozetkalar yoki fayl tavsiflari bilan bog'laydi. Masofadagi ma'lumotlar bazasi serverlariga ulanish holatida, keyingi manbalar serverda, masalan, hozirda ochilgan ResultSets uchun kursorlarga ulanadi. Har qanday JDBC ob'ekti o'z rolini o'ynashi bilanoq uni yopish juda muhimdir; axlat yig'ilishiga ishonmaslik kerak. Yuqorida sinab ko'rilgan manbalar tuzilishi buni oldini oladigan kod naqshidir. Ma'lumotlar bazasidan so'rovlar mexanizmi yordamida ma'lumotlar bazasidan olinadi. Quyidagi misolda bayonot yaratish va so'rovni bajarish ko'rsatilgan.
|
| |