0Pricing
Supabase Backend as a Service · レッスン

基本的なデータの挿入とクエリ

SupabaseクライアントライブラリとSQLエディターを使って、新しいレコードの挿入、データの取得、結果のフィルタリングを行う方法を学びます。

「基本的なデータの挿入とクエリ」はCoddyKit上の無料Supabase Backend as a Serviceレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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!

よくある質問

「基本的なデータの挿入とクエリ」レッスンは無料ですか?

はい。「基本的なデータの挿入とクエリ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Supabase Backend as a Serviceコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Supabase Backend as a Serviceコースには全4レッスンが含まれています。

「基本的なデータの挿入とクエリ」で何を学びますか?

SupabaseクライアントライブラリとSQLエディターを使って、新しいレコードの挿入、データの取得、結果のフィルタリングを行う方法を学びます。 ブラウザで直接実行するハンズオンコードでSupabase Backend as a Serviceを演習し、24時間対応の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に戻る