0Pricing
Supabase Backend as a Service · 课时

数据插入与查询基础

了解如何使用 Supabase 客户端库和 SQL 编辑器插入新记录、检索数据并筛选结果。

数据插入与查询基础 是 CoddyKit 上的免费 Supabase Backend as a Service 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Supabase Backend as a Service 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Supabase Backend as a Service 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Data Ops: Insert & Query

Welcome! In this lesson, we'll master the basics of interacting with your Supabase database. We'll focus on two crucial operations:

  • Inserting Data: Adding new records.
  • Querying Data: Retrieving existing information.

These skills are essential for building any dynamic application.

Supabase Client: Your Gateway

To interact with your database, you'll use the Supabase client library. Think of it as your application's direct line to Supabase!

For all our examples, we'll assume your supabase client is already initialized and ready. We'll focus on the specific database operations.

Add Data: The `insert()` Method

To add a new row (record) to your table, you'll use the insert() method. You pass it an object where keys match your table's column names.

Supabase handles assigning a unique ID and other default values automatically.

Example: Insert a Single User

Let's insert a new user into a users table. We'll set their name and email. Try running this!

import { createClient } from '@supabase/supabase-js';

// Placeholder client for demonstration
const supabase = createClient('https://your-project.supabase.co', 'your-anon-key');

async function main() {
  console.log("Attempting to insert a single user...");
  const { data, error } = await supabase
    .from('users')
    .insert({
      name: 'Alice Smith',
      email: 'alice@example.com'
    });

  if (error) {
    console.error('Error inserting user:', error.message);
  } else {
    console.log('User inserted successfully:', data);
  }
}

main();

Insert Many: Add Multiple Rows

What if you need to add several records at once? No problem! The insert() method also accepts an array of objects.

Each object in the array represents a new row to be added to your table.

Example: Insert Multiple Users

Here's how to insert two new users in a single operation. Notice we pass an array of objects to insert().

Run this to see it in action!

import { createClient } from '@supabase/supabase-js';

// Placeholder client for demonstration
const supabase = createClient('https://your-project.supabase.co', 'your-anon-key');

async function main() {
  console.log("Attempting to insert multiple users...");
  const { data, error } = await supabase
    .from('users')
    .insert([
      { name: 'Bob Johnson', email: 'bob@example.com' },
      { name: 'Charlie Brown', email: 'charlie@example.com' }
    ]);

  if (error) {
    console.error('Error inserting users:', error.message);
  } else {
    console.log('Users inserted successfully:', data);
  }
}

main();

Get Data: The `select()` Method

To fetch data from your table, you use the select() method. The simplest way to get all columns from all rows is to pass '*' (an asterisk).

This is your go-to for showing lists of data.

Example: Select All Users

Let's retrieve all the users we've inserted into our users table. The data variable will contain an array of user objects.

Run this code to see all users!

import { createClient } from '@supabase/supabase-js';

// Placeholder client for demonstration
const supabase = createClient('https://your-project.supabase.co', 'your-anon-key');

async function main() {
  console.log("Attempting to select all users...");
  const { data, error } = await supabase
    .from('users')
    .select('*');

  if (error) {
    console.error('Error selecting users:', error.message);
  } else {
    console.log('All users:', data);
  }
}

main();

Filter Data: `eq()` for Precision

Often, you don't need all data. You want specific records. The eq() method allows you to filter results where a column's value equals a specific value.

It's perfect for finding a user by their ID or email.

Example: Filter Users by Email

Let's find a user with a specific email address. We'll use eq('email', 'alice@example.com') to narrow down our search.

Run this and see if Alice is found!

import { createClient } from '@supabase/supabase-js';

// Placeholder client for demonstration
const supabase = createClient('https://your-project.supabase.co', 'your-anon-key');

async function main() {
  console.log("Attempting to find user by email...");
  const { data, error } = await supabase
    .from('users')
    .select('*')
    .eq('email', 'alice@example.com');

  if (error) {
    console.error('Error finding user:', error.message);
  } else {
    console.log('User found:', data);
  }
}

main();

Quick Check: Insert & Query

Imagine you have a products table. You want to add a new product named "Keyboard" with a price of 75, and then immediately retrieve all products that have a price of 75.

Which sequence of Supabase client calls is correct?

Recap: Insert & Query Mastery

Great job! You've learned the fundamental operations for managing data in Supabase:

  • .insert(): For adding one or many new records to a table.
  • .select('*'): For retrieving all columns from all rows.
  • .eq(): For filtering your queries to find specific records.

These methods form the backbone of interacting with your Supabase database. Next, we'll explore more advanced querying techniques!

常见问题解答

「数据插入与查询基础」课时是免费的吗?

是的 — 「数据插入与查询基础」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Supabase Backend as a Service 课程的其余内容,请升级到 CoddyKit PRO。 Supabase Backend as a Service 课程共包含 4 节课。

「数据插入与查询基础」这节课中我会学到什么?

了解如何使用 Supabase 客户端库和 SQL 编辑器插入新记录、检索数据并筛选结果。 你通过在浏览器中直接运行的动手代码来练习 Supabase Backend as a Service,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Supabase Backend as a Service 需要有经验吗?

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

「数据插入与查询基础」课时需要多长时间?

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

我能在这节 Supabase Backend as a Service 课中编写并运行代码吗?

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

此课程中的所有课时

  1. 设计数据库架构
  2. 创建表和列
  3. 数据插入与查询基础
  4. 关系与外键
← 返回 Supabase Backend as a Service