sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

mssql2005 Varchar(200) cannot use the "get" method correctly in version 0.4

Open templar721 opened this issue 5 years ago • 1 comments

Hello, I have a question here. When the field in mssql is varchar and the size is 200, when using the mssqlrow.get method to a String, the following error occurs: thread'tokio-runtime-worker' panicked at'called Result::unwrap() on an ʻErr` value: ColumnDecode {index: ""PersonName"", source: Protocol("unsupported locale 0x2052")} ' If the "get" method can be executed correctly when changing varchar to nvarchar, I pray you can solve this problem. Version is sqlx = {version = "0.4.0-beta.1", default-features = false, features = ["runtime-tokio", "macros","mssql"]} thank a lot

templar721 avatar Nov 06 '20 14:11 templar721

use sqlx::mssql::MssqlRow;
use sqlx::Row;

fn decode_varchar(row: &MssqlRow, column_name: &str) -> Result<String, sqlx::Error> {
    let value: Option<String> = row.try_get(column_name)?;
    value.ok_or_else(|| sqlx::Error::ColumnDecode {
        index: column_name.to_string(),
        source: sqlx::protocol::Error::from("Failed to decode value"),
    })
}

#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
    let pool = sqlx::MssqlPool::connect("mssql://username:password@localhost/database").await?;
    let row = sqlx::query("SELECT PersonName FROM YourTable")
        .fetch_one(&pool)
        .await?;

    match decode_varchar(&row, "PersonName") {
        Ok(name) => println!("PersonName: {}", name),
        Err(e) => eprintln!("Error: {}", e),
    }

    Ok(())
}

ljluestc avatar Sep 14 '24 18:09 ljluestc