0

Using rust sqlx sqlx = { version = "0.8.6", features = ["sqlite", "runtime-tokio", "macros"] } . I am using a SQLite Db for my application. After making a db connection pool and performing a query , when i try to print the result of the query i am unable to do so.

Code :-

 let db_url = crate::utils::env::db_url();
 let pool = sqlx::sqlite::SqlitePoolOptions::new()
        .max_connections(5)
        .connect(&db_url)
        .await
        .expect("db connection error");

 let rows = sqlx::query(query_string)
    .bind(&id) 
    .fetch_all(&pool)
    .await
    .expect("db query error");

 println!("Query result - {:#?}", rows); //error is shown here

Error :-

SqliteRow` doesn't implement `Debug`
the trait `Debug` is not implemented for `SqliteRow`
the trait `Debug` is implemented for `Vec<T, A>

2 Answers 2

0

Short answer: As of version 0.8.6, SqliteRow simply does not implement Debug, and there is nothing one can do about that within 0.8.6.

It seems a std::fmt::Debug implementation found its way into sqlx very recently, but as of today that feature seems unreleased.

Sign up to request clarification or add additional context in comments.

Comments

0

This happens because SqliteRow does not implement Debug , so unable to display using println!() .
We need to extract the fields from each row manually and then print, this can be done using a for loop :

use sqlx::Row; //also add this

//....prevoius code---//
 let rows = sqlx::query(query_string)
    .bind(&id) 
    .fetch_all(&pool)
    .await
    .expect("db query error");

// loop through rows and print values
for row in rows {
    let name: String = row.get("name");
    let id: String = row.get("id");

    println!(
        "Name: {}, ID: {}",
        name, id
    );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.