Veritabanı Entegrasyonu (SQLx/Diesel)
SQLx veya Diesel gibi popüler ORM'ler ya da sorgu oluşturucuları kullanarak Rust web hizmetinizi veritabanlarına entegre edin.
Veritabanı Entegrasyonu (SQLx/Diesel), CoddyKit'te ücretsiz bir Learn Rust Coding dersidir. Bu, 3 dersinin 2. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Learn Rust Coding öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Learn Rust Coding kursu toplamda 3 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
Why Databases for Web?
Web services often need to store and retrieve data persistently. Think about user profiles, product catalogs, or blog posts – this data needs to live somewhere beyond your program's memory.
Databases provide a structured way to manage this data, ensuring it's safe, consistent, and accessible. Integrating your Rust web service with a database is a core task for most real-world applications.
SQLx: Your Async DB Friend
In Rust, we have excellent tools for database interaction. Two popular choices are SQLx and Diesel.
- SQLx: An asynchronous, pure Rust SQL crate with compile-time checked queries. It's often preferred for async web services built with frameworks like Tokio.
- Diesel: A powerful ORM (Object-Relational Mapper) that provides a more abstract way to interact with your database, mapping Rust structs directly to database tables. It's typically synchronous.
For this lesson, we'll focus on SQLx due to its strong async support, which aligns well with modern Rust web service development.
SQLx Setup: Dependencies
Before writing code, you need to add sqlx and tokio to your project's Cargo.toml. tokio provides the async runtime needed by sqlx.
For example, to use SQLite (an easy file-based database for local development) with SQLx, your Cargo.toml might look like this:
[dependencies]
sqlx = { version = "0.7", features = ["runtime-tokio-rustls", "sqlite"] }
tokio = { version = "1", features = ["full"] }You'll also typically use a DATABASE_URL environment variable to configure your database connection string.
Connect to Your First DB
Let's establish a connection to an in-memory SQLite database. SQLx uses connection pools, which manage multiple database connections efficiently to handle concurrent requests in your web service.
Try running this example:
use sqlx::SqlitePool;
use tokio;
#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
// Use an in-memory SQLite database for simplicity
let database_url = "sqlite::memory:";
let pool = SqlitePool::connect(&database_url).await?;
println!("Successfully connected to the database!");
// In a real web app, 'pool' would be passed around or stored in app state.
Ok(())
}Structuring Data with Tables
Before we can store data, we need to define our database schema using DDL (Data Definition Language). Let's create a simple tasks table.
The sqlx::query! macro allows you to embed SQL directly into your Rust code. It even performs compile-time checks on your SQL!
use sqlx::{SqlitePool, query};
use tokio;
#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
let database_url = "sqlite::memory:";
let pool = SqlitePool::connect(&database_url).await?;
// Create a 'tasks' table if it doesn't exist
query!(
"CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
description TEXT NOT NULL,
completed BOOLEAN NOT NULL DEFAULT FALSE
)"
).execute(&pool).await?;
println!("Table 'tasks' created successfully!");
Ok(())
}Adding New Records
Now that we have a table, let's add some data! We'll use the INSERT statement with parameter binding. This protects against SQL injection and makes your queries safer.
Notice how ? placeholders are used for parameters, and values are passed to .bind().
use sqlx::{SqlitePool, query};
use tokio;
#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
let database_url = "sqlite::memory:";
let pool = SqlitePool::connect(&database_url).await?;
query!(
"CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
description TEXT NOT NULL,
completed BOOLEAN NOT NULL DEFAULT FALSE
)"
).execute(&pool).await?;
// Insert a new task
let description = "Learn SQLx";
let completed = false;
query!(
"INSERT INTO tasks (description, completed) VALUES (?, ?)",
description,
completed
).execute(&pool).await?;
println!("Task '{}' inserted successfully!", description);
Ok(())
}Retrieving Data with SELECT
To fetch data from our tasks table, we use the SELECT statement. SQLx provides methods like .fetch_all() or .fetch_one() to get results.
You can iterate over the returned rows and access columns by name or index.
use sqlx::{SqlitePool, query};
use tokio;
#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
let database_url = "sqlite::memory:";
let pool = SqlitePool::connect(&database_url).await?;
query!(
"CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
description TEXT NOT NULL,
completed BOOLEAN NOT NULL DEFAULT FALSE
)"
).execute(&pool).await?;
query!(
"INSERT INTO tasks (description, completed) VALUES (?, ?)",
"Learn SQLx", false
).execute(&pool).await?;
query!(
"INSERT INTO tasks (description, completed) VALUES (?, ?)",
"Build a web app", false
).execute(&pool).await?;
// Fetch and print all tasks
println!("\n--- All Tasks ---");
let rows = query!("SELECT id, description, completed FROM tasks")
.fetch_all(&pool).await?;
for row in rows {
println!("ID: {}, Desc: {}, Done: {}",
row.id, row.description, row.completed);
}
Ok(())
}Type-Safe Data with Structs
Manually extracting data from rows can be error-prone. SQLx's query_as! macro, combined with #[derive(FromRow)], lets you map query results directly into Rust structs!
This provides strong type safety and makes your code much cleaner and easier to work with.
use sqlx::{FromRow, SqlitePool, query, query_as};
use tokio;
#[derive(Debug, FromRow)]
struct Task {
id: i64,
description: String,
completed: bool,
}
#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
let database_url = "sqlite::memory:";
let pool = SqlitePool::connect(&database_url).await?;
query!(
"CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
description TEXT NOT NULL,
completed BOOLEAN NOT NULL DEFAULT FALSE
)"
).execute(&pool).await?;
query!(
"INSERT INTO tasks (description, completed) VALUES (?, ?)",
"Learn SQLx", false
).execute(&pool).await?;
// Fetch tasks directly into our Task struct
println!("\n--- Tasks as Structs ---");
let tasks = query_as::<_, Task>("SELECT id, description, completed FROM tasks")
.fetch_all(&pool).await?;
for task in tasks {
println!("{:?}", task);
}
Ok(())
}Modifying & Deleting Records
Updating and deleting data follows a similar pattern to inserting. You use UPDATE and DELETE SQL statements with parameter binding.
- UPDATE: Changes existing rows based on a
WHEREclause. - DELETE: Removes rows based on a
WHEREclause.
Always be careful with WHERE clauses in UPDATE and DELETE to avoid unintended data loss!
use sqlx::{SqlitePool, query};
use tokio;
#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
let database_url = "sqlite::memory:";
let pool = SqlitePool::connect(&database_url).await?;
query!(
"CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
description TEXT NOT NULL,
completed BOOLEAN NOT NULL DEFAULT FALSE
)"
).execute(&pool).await?;
query!(
"INSERT INTO tasks (description, completed) VALUES (?, ?)",
"Finish lesson", false
).execute(&pool).await?;
let task_id_to_update = 1;
// Update a task
query!(
"UPDATE tasks SET completed = ? WHERE id = ?",
true,
task_id_to_update
).execute(&pool).await?;
println!("Task {} updated.", task_id_to_update);
// Delete a task (uncomment to run, careful with IDs after update)
// let task_id_to_delete = 1;
// query!(
// "DELETE FROM tasks WHERE id = ?",
// task_id_to_delete
// ).execute(&pool).await?;
// println!("Task {} deleted.", task_id_to_delete);
Ok(())
}Test Your DB Skills
Which of the following statements are TRUE regarding SQLx in Rust web services?
DB Integration: Key Takeaways
You've taken a significant step in building robust Rust web services by learning database integration!
- Persistence: Databases are vital for storing application data.
- SQLx: An async, compile-time checked SQL crate, great for web services.
- Connection Pools: Essential for efficient and concurrent database access.
- CRUD: We covered creating (
INSERT), retrieving (SELECT), updating (UPDATE), and deleting (DELETE) data. - Type Safety:
query_as!and#[derive(FromRow)]enable safe and ergonomic data mapping.
Next, you can explore integrating these database operations into actual web routes!
Sıkça Sorulan Sorular
“Veritabanı Entegrasyonu (SQLx/Diesel)” dersi ücretsiz mi?
Evet — “Veritabanı Entegrasyonu (SQLx/Diesel)” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Learn Rust Coding kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Learn Rust Coding kursu toplamda 3 dersten oluşur.
“Veritabanı Entegrasyonu (SQLx/Diesel)” dersinde ne öğreneceğim?
SQLx veya Diesel gibi popüler ORM'ler ya da sorgu oluşturucuları kullanarak Rust web hizmetinizi veritabanlarına entegre edin. Learn Rust Coding ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Learn Rust Coding öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Learn Rust Coding, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 3 dersinin 2. dersidir.
“Veritabanı Entegrasyonu (SQLx/Diesel)” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Learn Rust Coding dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Learn Rust Coding dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Actix-web/Rocket ile REST API'leri
- Veritabanı Entegrasyonu (SQLx/Diesel)
- Kimlik Doğrulama ve Yetkilendirme