Function fetch

Source
pub fn fetch<T: SqlQuery + FromRow + SqlParams>(
    client: &mut Client,
    params: &T,
) -> Result<T, Error>
Expand description

§fetch

Retrieves a single record from the database.

§Parameters

  • client: Database connection client
  • params: Query parameters (must implement SqlQuery, FromRow, and SqlParams traits)

§Return Value

  • Result<T, Error>: On success, returns the retrieved record; on failure, returns Error

§Struct Definition

Structs used with this function should be annotated with the following derive macros:

#[derive(Queryable, FromRow, SqlParams)]  // Required macros
#[table("table_name")]                    // Table name to query
#[where_clause("id = $1")]                // WHERE clause with parameter placeholders
struct GetUser {
    id: i32,                              // Parameter for the WHERE clause
    name: String,                         // Field to retrieve
    email: String,                        // Field to retrieve
}