Sqlx: run example get error

Created on 14 May 2020  路  2Comments  路  Source: launchbadge/sqlx

use std::env;

 use sqlx::mysql::MySqlPool;

#[async_std::main] // or #[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
    // Create a connection pool
    let pool = MySqlPool::builder()
        .max_size(5) // maximum number of connections in the pool
        .build(&env::var("DATABASE_URL")?).await?;

    // Make a simple query to return the given parameter
    let row: (i64,) = sqlx::query_as("SELECT $1")
        .bind(150_i64)
        .fetch_one(&pool).await?;

    assert_eq!(row.0, 150);

    Ok(())
}

get error

error[E0277]: `?` couldn't convert the error to `sqlx_core::error::Error`
  --> examples/mysql/todos/src/main.rs:10:41
   |
10 |         .build(&env::var("DATABASE_URL")?).await?;
   |                                         ^ the trait `std::convert::From<std::env::VarError>` is not implemented for `sqlx_core::error::Error`
   |
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
   = help: the following implementations were found:
             <sqlx_core::error::Error as std::convert::From<sqlx_core::error::ProtocolError<'_>>>
             <sqlx_core::error::Error as std::convert::From<sqlx_core::error::TlsError<'_>>>
             <sqlx_core::error::Error as std::convert::From<sqlx_core::error::UnexpectedNullError>>
             <sqlx_core::error::Error as std::convert::From<sqlx_core::mysql::error::MySqlError>>
           and 3 others
   = note: required by `std::convert::From::from`

error[E0599]: no method named `fetch_one` found for struct `sqlx_core::query_as::QueryAs<'_, sqlx_core::mysql::database::MySql, _>` in the current scope
   --> examples/mysql/todos/src/main.rs:15:10
    |
15  |         .fetch_one(&pool).await?;
    |          ^^^^^^^^^ method not found in `sqlx_core::query_as::QueryAs<'_, sqlx_core::mysql::database::MySql, _>`
    | 
   ::: /Users/edison/code/rust/sqlx/sqlx-core/src/query_as.rs:100:16
    |
100 |             fn fetch_one<'e, E>(
    |                ---------
    |                |
    |                the method is available for `std::boxed::Box<sqlx_core::query_as::QueryAs<'_, sqlx_core::mysql::database::MySql, _>>` here
    |                the method is available for `std::sync::Arc<sqlx_core::query_as::QueryAs<'_, sqlx_core::mysql::database::MySql, _>>` here
    |                the method is available for `std::rc::Rc<sqlx_core::query_as::QueryAs<'_, sqlx_core::mysql::database::MySql, _>>` here
    |
    = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
    |
1   | use sqlx_core::mysql::MySqlQueryAs;
    |

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0277, E0599.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `sqlx-example-mysql-todos`.

Most helpful comment

I personally found it very confusing that the very first example in the README doesn't even compile. Any reason the example in the README doesn't use anyhow::Result?

All 2 comments

sqlx::Error is not meant to be a generic error for application-level use. This is why our examples use anyhow::Result instead of Result<..., sqlx::Error>.

As for the second error, the suggested solution is the correct one, although it's pointing to the wrong crate; please add an import for sqlx::mysql::MySqlQueryAs.

I personally found it very confusing that the very first example in the README doesn't even compile. Any reason the example in the README doesn't use anyhow::Result?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DarrienG picture DarrienG  路  4Comments

abonander picture abonander  路  5Comments

kunjee17 picture kunjee17  路  3Comments

neo-clon picture neo-clon  路  4Comments

sam701 picture sam701  路  6Comments