บันทึกและอ่านระเบียน
เชื่อมกระบวนการสร้างและอ่านข้อมูล
บันทึกและอ่านระเบียน เป็นบทเรียน Vibe Coding ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Vibe Coding และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Vibe Coding มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Four Core Actions
Almost everything an app does with data is one of four actions: Create, Read, Update, and Delete. Together they're called CRUD.
Once your tables exist, you'll ask the AI to write code for each of these, often one feature at a time.
Saving a New Record
To store something new, you insert a row into a table. The app collects the values, then writes them into the database.
Describe the data and the table, and let the AI generate the save function with the right fields filled in.
Write a function that saves a new task to the "tasks" table.
It should take a title and a due date, and return the newly created task's id.Reading Records Back
Reading means fetching rows out of the database. You might want all of them, or just the ones that match a condition.
Be clear about what you want back and in what order, so the AI writes a query that returns exactly the right rows.
Write a query that reads all tasks for a given user id,
sorted by due date with the soonest first.Filtering With Conditions
Often you don't want every row, just the matching ones. Filtering uses conditions like "where status is open" or "where created after Monday."
State the filter plainly and the AI turns it into the proper query, whether that's SQL or an ORM call.
Read only the tasks that are not completed and are due within the next 7 days.
Return the title and due date for each.Updating a Record
To change existing data, you update a row by finding it (usually by id) and setting new values.
Always tell the AI how to identify the right row, so the update touches only that record and not the whole table.
Write a function that marks a task as completed.
It takes a task id and sets the completed flag to true for only that row.Deleting Carefully
Deleting removes a row for good. It's powerful and easy to get wrong, so be precise about which row.
Many apps prefer a "soft delete," flipping an is_deleted flag instead of truly removing the data. Ask the AI which fits your case.
I want to delete a task by id, but keep it recoverable.
Implement a soft delete using an is_deleted flag instead of removing the row.Never Trust Raw Input
Data from users can be messy or even malicious. Inserting it directly into a query can cause errors or security holes like SQL injection.
Modern tools handle this with parameterized queries. Remind the AI to use safe, parameterized inserts rather than gluing strings together.
When you write database code for this app,
always use parameterized queries so user input can't cause SQL injection.Handling the Empty Case
What happens when a read finds nothing? A search with no matches should show "no results," not crash.
Ask the AI to handle the empty case so your app behaves gracefully when the database returns zero rows.
Update the task-reading function so that when no tasks match,
it returns an empty list and the UI shows a friendly "nothing here yet" message.Reads Should Be Fast
As tables grow, reads can slow down. An index is like a book's index: it helps the database find rows quickly.
If a frequent query feels slow, ask the AI whether adding an index on the filtered column would help.
My query that filters tasks by user_id is getting slow.
Would an index on user_id help, and if so, generate the migration to add it.Testing Your CRUD
Before trusting your data layer, test it. Save a record, read it back, update it, then delete it, and confirm each step worked.
You can ask the AI to write these tests so future changes don't quietly break saving or reading.
Write tests that create a task, read it back, update its title, then delete it,
checking the database state is correct after each step.Tying It Together
With CRUD in place, your app finally remembers. Users create data, see it again, change it, and remove it, all backed by your database.
From here, every new feature is just another combination of saving and reading records.
Quick Check
Test your understanding of saving and reading records.
Recap
CRUD covers Create, Read, Update, and Delete: the four core data actions. You learned to save rows, read and filter them, update by id, and delete carefully, often with a soft delete.
Always use parameterized queries, handle empty results, index slow reads, and test your data layer. You can now add a working database to your AI app.
เรียนรู้ JavaScript ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 25
- บทเรียน
- 100
คำถามที่พบบ่อย
บทเรียน “บันทึกและอ่านระเบียน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “บันทึกและอ่านระเบียน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Vibe Coding ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Vibe Coding มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “บันทึกและอ่านระเบียน”
เชื่อมกระบวนการสร้างและอ่านข้อมูล คุณปฏิบัติ Vibe Coding ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Vibe Coding หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Vibe Coding บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “บันทึกและอ่านระเบียน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Vibe Coding นี้ได้ไหม
ได้ บทเรียน Vibe Coding ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุใดแอปจึงต้องใช้ข้อมูล
- เลือกฐานข้อมูลด้วยคำสั่ง
- การสร้างแบบจำลองตารางด้วยปัญญาประดิษฐ์
- บันทึกและอ่านระเบียน