在Rust中使用ODBC Crate连接到SQL Server时出错

dfddblmv  于 2023-11-19  发布在  SQL Server
关注(0)|答案(1)|浏览(218)

有人知道如何解决这个问题吗?尝试使用Rust创建一个REST API(用于API和ODBC的warp crate-API Crate连接到MS SQL Server)。
验证码:

use odbc_api::{ConnectionOptions, Environment, Cursor};
use serde::Serialize;
use warp::{Filter, Rejection, Reply};

#[derive(Serialize)]
struct Data {
    name: String,
    price: f64,
    quantity: i32,
}

async fn fetch_data() -> Result<Vec<Data>, odbc_api::Error> {
    let env = Environment::new()?;
    let connection_string = "Driver={ODBC Driver 17 for SQL Server};Server=Connection_String ..."; // Connection details
    let mut conn = env.connect_with_connection_string(connection_string, ConnectionOptions::default())?;
    let sql = "SELECT name, price, quantity 
    FROM dbo.tblGrocery
    WHERE status = 1
    ORDER BY name ASC"; // SQL query
    let cursor = conn.execute(sql, ())?;
    let mut data = Vec::new();
    if let Some(cursor) = cursor {
        while let Some(row) = cursor.next_row()? {
            let name: String = row.get_col(1)?;
            let price: f64 = row.get_col(2)?;
            let quantity: i32 = row.get_col(3)?;
            data.push(Data { name, price, quantity });
        }
    }
    Ok(data)
}

async fn handle_request() -> Result<impl Reply, Rejection> {
    let data = fetch_data().await.unwrap();
    Ok(warp::reply::json(&data))
}

#[tokio::main]
async fn main() {
    let route = warp::get().and(warp::path("data")).and_then(handle_request);
    warp::serve(route).run(([127, 0, 0, 1], 3030)).await;
}

字符串
犯这个错误

error[E0599]: no method named `get_col` found for struct `CursorRow` in the current scope
  --> src\main.rs:24:36
   |
24 |             let name: String = row.get_col(1)?;
   |                                    ^^^^^^^ method not found in `CursorRow<'_>`      

error[E0599]: no method named `get_col` found for struct `CursorRow` in the current scope
  --> src\main.rs:25:34
   |
25 |             let price: f64 = row.get_col(2)?;
   |                                  ^^^^^^^ method not found in `CursorRow<'_>`

error[E0599]: no method named `get_col` found for struct `CursorRow` in the current scope
  --> src\main.rs:26:37
   |
26 |             let quantity: i32 = row.get_col(3)?;
   |                                     ^^^^^^^ method not found in `CursorRow<'_>`


我的Cargo.toml

[package]
name = "neat-api"
version = "0.1.0"
edition = "2021"

[dependencies]
odbc-api = "0.57.0"
warp = "0.2"
serde = { version = "1.0", features = ["derive"] }

lb3vh1jj

lb3vh1jj1#

get_col方法不存在。您可以改用get_text。请参阅https://docs.rs/odbc-api/3.0.1/odbc_api/struct.CursorRow.html#method.get_text

相关问题