sqlx
sqlx copied to clipboard
mssql2005 Varchar(200) cannot use the "get" method correctly in version 0.4
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
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(())
}