0Pricing
Supabase Backend as a Service · Lekcja

Podstawowe wstawianie danych i wykonywanie zapytań

Dowiedz się, jak wstawiać nowe rekordy, pobierać dane i filtrować wyniki za pomocą biblioteki klienckiej Supabase oraz edytora SQL.

Podstawowe wstawianie danych i wykonywanie zapytań to bezpłatna lekcja Supabase Backend as a Service na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Supabase Backend as a Service, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Supabase Backend as a Service zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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!

Często zadawane pytania

Czy lekcja „Podstawowe wstawianie danych i wykonywanie zapytań” jest bezpłatna?

Tak — pełny tekst „Podstawowe wstawianie danych i wykonywanie zapytań” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Supabase Backend as a Service, przejdź na CoddyKit PRO. Kurs Supabase Backend as a Service zawiera 4 lekcji w sumie.

Co nauczysz się w „Podstawowe wstawianie danych i wykonywanie zapytań”?

Dowiedz się, jak wstawiać nowe rekordy, pobierać dane i filtrować wyniki za pomocą biblioteki klienckiej Supabase oraz edytora SQL. Ćwiczysz Supabase Backend as a Service z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Supabase Backend as a Service?

Nie wymagamy żadnego doświadczenia. Supabase Backend as a Service w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.

Ile czasu zajmuje lekcja „Podstawowe wstawianie danych i wykonywanie zapytań”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Supabase Backend as a Service?

Tak. Każda lekcja Supabase Backend as a Service zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Projektowanie schematów baz danych
  2. Tworzenie tabel i kolumn
  3. Podstawowe wstawianie danych i wykonywanie zapytań
  4. Relacje i klucze obce
← Powrót do Supabase Backend as a Service