How to deal with null in Postgres query

what is the best way to handle the null in Postgres? in the code below, application 2's display_name is null in Postgres.

use postgres::{Client, Error, NoTls};

fn print_application_data() {
    let connect_str = "host=localhost dbname=test";
    let mut client = Client::connect(&connect_str, NoTls)?;
    let query = "SELECT id, display_name FROM applications WHERE id IN (1,2)";
    let rows = client.query(query, &[])?;
 
    for row in rows {
        let id: i64 = row.get("id");
        let display_name: &str = row.get("display_name");
        println!("id: {}, display_name: {}", id, display_name);
    }
}

id: 1, display_name: Chrome
thread 'main' panicked at error retrieving column display_name: 
error deserializing column 1: a Postgres value was `NULL`',

As usual in Rust, use Option:

let display_name: Option<&str> = row.get("display_name");
1 Like

@jdahlstrom , thank you! that worked

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.