Vibe Coding · 课时

保存和读取记录

连接创建和读取流程。

第 4 / 4 课13 个步骤

保存和读取记录 是 CoddyKit 上的免费 Vibe Coding 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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.

免费开始

用 AI 导师学习 JavaScript — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
25
课程
100

常见问题解答

「保存和读取记录」课时是免费的吗?

是的 — 「保存和读取记录」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Vibe Coding 课程的其余内容,请升级到 CoddyKit PRO。 Vibe Coding 课程共包含 4 节课。

「保存和读取记录」这节课中我会学到什么?

连接创建和读取流程。 你通过在浏览器中直接运行的动手代码来练习 Vibe Coding,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Vibe Coding 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Vibe Coding 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「保存和读取记录」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Vibe Coding 课中编写并运行代码吗?

能。每节 Vibe Coding 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 应用为何需要数据
  2. 通过提示词选择数据库
  3. 使用人工智能建模数据表
  4. 保存和读取记录
← 返回 Vibe Coding