0Pricing
Flask Academy · บทเรียน

สร้างระเบียนและยืนยันเซสชัน

เพิ่มออบเจ็กต์และบันทึกถาวรด้วยเซสชัน

สร้างระเบียนและยืนยันเซสชัน เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flask Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

The C in CRUD

Creating a record means turning incoming data into a new row in your table. In Flask-SQLAlchemy that flow runs through the session. ✨

Build the Object First

Start by making a fresh model instance with the values you want. Nothing has touched the database yet, it is just a Python object.

user = User(name="Ada", email="ada@mail.com")

Add It to the Session

Tell SQLAlchemy to track the new object by calling db.session.add(). This stages it, but it is still not saved.

db.session.add(user)

Commit to Save

Nothing is permanent until you call db.session.commit(). That flushes your changes and writes the row to the database.

db.session.commit()

The Id Appears After Commit

Once you commit, the database assigns a primary key. Read it back from user.id to confirm the row really exists.

db.session.commit()
print(user.id)

A Full Create Endpoint

Inside a POST view you read the body, build the object, add it, and commit. That is the whole create path in four lines.

data = request.get_json()
user = User(name=data["name"])
db.session.add(user)
db.session.commit()

Return 201 Created

A successful create should answer with status 201, the code that means a new resource was made on the server.

return jsonify(id=user.id), 201

Roll Back on Failure

If a commit fails, wrap it in try and call db.session.rollback() to undo the staged change and keep your session clean.

try:
    db.session.commit()
except Exception:
    db.session.rollback()

Add Many at Once

Need several rows? Stage them all with add_all(), then a single commit saves the whole batch together.

db.session.add_all([u1, u2, u3])
db.session.commit()

One Commit, One Transaction

Every add since the last commit is part of one transaction. They all succeed together, or none of them land if it fails.

Add Stages, Commit Saves

Remember the rhythm: build the object, add to stage it, then commit to save. Skipping commit means your data never persists.

Quick Check

You called add() but the row is missing from the table. What did you forget?

Recap: Creating Records

You build an object, add it, and commit to save, returning 201 and rolling back on errors. That is a solid create endpoint! 🎉

คำถามที่พบบ่อย

บทเรียน “สร้างระเบียนและยืนยันเซสชัน” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “สร้างระเบียนและยืนยันเซสชัน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “สร้างระเบียนและยืนยันเซสชัน”

เพิ่มออบเจ็กต์และบันทึกถาวรด้วยเซสชัน คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “สร้างระเบียนและยืนยันเซสชัน” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม

ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. สร้างระเบียนและยืนยันเซสชัน
  2. ปลายทางอ่านหนึ่งรายการและอ่านหลายรายการ
  3. อัปเดตระเบียนเดิมอย่างปลอดภัย
  4. ลบและจัดการแถวที่หายไป
← กลับไปที่ Flask Academy