blackbeam / rust-mysql-simple

Mysql client library implemented in rust.
Apache License 2.0
665 stars 146 forks source link

How to handle and error from executes insert data? #196

Closed rchatsiri closed 4 years ago

rchatsiri commented 4 years ago

I'm insert data into stmt.execute() as example below, but database found an error from duplicate primary key and send an error back to execute function. How to handle an error from match keyword.

let result =stmt.execute(params! { 
    "user_id" => user_ac.user_id,
    "pub_key_temp" => user_ac.pub_key_temp,
    }).unwarp();
 match result {
   .... How to define handler exception from here(mysql::QueryResult<'s>)?... 
 }
blackbeam commented 4 years ago

Hi!

The MySqlError type exposes state, message and code fields:

match stmt.execute(params! { /* ... */ }) {
    Ok(_) => (/* inserted */),
    Err(my::Error::MySqlError(err)) => (/* your handler logic here */),
}

I would suggest, however, to consider other approaches. See here for example.