ติดตั้งและกำหนดค่าส่วนขยาย
กำหนด URI ของฐานข้อมูลและเริ่มต้น SQLAlchemy
ติดตั้งและกำหนดค่าส่วนขยาย เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flask Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why an ORM at All
Writing raw SQL by hand gets messy fast. An ORM lets you work with Python objects and rows feel like normal classes. 🗃️
Meet Flask-SQLAlchemy
Flask-SQLAlchemy is the official extension that wires SQLAlchemy into Flask, so your app gets a clean, ready-to-use database layer.
Install the Package
You add it like any dependency. pip pulls in Flask-SQLAlchemy and the SQLAlchemy core it builds on automatically.
pip install Flask-SQLAlchemyCreate the db Object
You make one SQLAlchemy instance, usually named db. Every model and query you write later will hang off this single object.
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()The Database URI
Flask needs to know where your data lives. The SQLALCHEMY_DATABASE_URI setting is a connection string pointing at your database.
A Simple SQLite URI
For learning, SQLite is perfect: it lives in one file, needs no server, and the URI is just a path on disk.
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///app.db"Bind db to the App
After config, you connect the extension to your app with init_app. This hands Flask-SQLAlchemy everything it needs to run.
db.init_app(app)Two Ways to Initialize
You can pass the app straight to SQLAlchemy(app) for tiny apps, or use init_app later to keep db reusable across setups.
Swap Databases Easily
Only the URI changes between SQLite, PostgreSQL, or MySQL. Your models and queries stay identical, which makes switching painless.
"postgresql://user:pass@localhost/mydb"Skip the Modification Tracker
Set SQLALCHEMY_TRACK_MODIFICATIONS to False. It silences a warning and avoids extra overhead you almost never need.
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = FalseOne Extension, Many Tools
Once configured, db gives you Model for tables, Column for fields, and session for saving. You will meet each of these soon.
Quick Check
Which config key tells Flask-SQLAlchemy where your database lives?
Recap
You installed the extension, created a db object, set the URI, and bound it with init_app. Your app is ready for models. 🎉
คำถามที่พบบ่อย
บทเรียน “ติดตั้งและกำหนดค่าส่วนขยาย” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ติดตั้งและกำหนดค่าส่วนขยาย” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ติดตั้งและกำหนดค่าส่วนขยาย”
กำหนด URI ของฐานข้อมูลและเริ่มต้น SQLAlchemy คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “ติดตั้งและกำหนดค่าส่วนขยาย” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม
ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ติดตั้งและกำหนดค่าส่วนขยาย
- กำหนดคลาสโมเดลแรกของคุณ
- คอลัมน์ ชนิดข้อมูล และข้อจำกัด
- สร้างแบบแผนด้วย create_all