Tiberius simple_query根据文档编译时错误

问题描述 投票:0回答:1

根据Tiberius documentation,使用SQLConnection::simple_query()函数很简单:

extern crate futures;
extern crate futures_state_stream;
extern crate tokio;
extern crate tiberius;
use futures::Future;
use futures_state_stream::StateStream;
use tokio::executor::current_thread;
use tiberius::SqlConnection;

fn main() {

   // 1: for windows we demonstrate the hardcoded variant
   // which is equivalent to:
   //     let conn_str = "server=tcp:localhost,1433;integratedSecurity=true;";
   //     let future = SqlConnection::connect(conn_str).and_then(|conn| {
   // and for linux we use the connection string from an environment variable
   let conn_str = if cfg!(windows) {
       "server=tcp:localhost,1433;integratedSecurity=true;".to_owned()
   } else {
       ::std::env::var("TIBERIUS_TEST_CONNECTION_STRING").unwrap()
   };

   let future = SqlConnection::connect(conn_str.as_str())
       .and_then(|conn| {
           conn.simple_query("SELECT 1+2").for_each(|row| {
               let val: i32 = row.get(0);
               assert_eq!(val, 3i32);
               Ok(())
           })
       })
       .and_then(|conn| conn.simple_exec("create table #Temp(gg int);"))
       .and_then(|(_, conn)| conn.simple_exec("UPDATE #Temp SET gg=1 WHERE gg=1"));

   current_thread::block_on_all(future).unwrap();
}

但是,我使用tiberius crate verson 0.3.2如下,我得到一个编译时错误。我不知道我做错了什么因为QueryResult类型并没有指出我需要包装什么类型的包装器(在QueryResult文档中没有手动编写的文档或示例,只有自动生成的API文档,这对于一个新的Rust程序员来说非常神秘!:()

我习惯用StdResultOption以及Ok()处理Err()s和Some()s,我觉得我需要在下面第7行做像Ok(rslt)这样的事情,但这也给了我一个编译时错误。

  • 问题1:文档是否正确?如果我的代码没有编译,我认为文档代码也不会......除非我使用的是错误版本的Rust(使用stable)或者除非Tiberius文档仅适用于某些旧版本的Tiberius 。
  • 问题2:如何修复编译时错误并执行与doc示例等效的操作?我只想运行一个简单的查询并将处理后的结果返回到字符串中。
fn get_users() -> String {
    let conn_str : &str = &::std::env::var("SQLCONN").expect("Could not obtain SQLCONN");
    let mut output = String::new();
    let first : bool = true;
    let future = SqlConnection::connect(conn_str)
    .and_then(|conn| {
        if let rslt = conn.simple_query("SELECT \"A\", \"B\"") {
            rslt.for_each(|row| {
                let account : &str = row.get(0);
                let charName : &str = row.get(1);
                if first == false {
                    output.push_str("\r\n");
                }
                output.push_str(account);
                output.push_str(" is logged in as ");
                output.push_str(charName);
                first = false;
                Ok(())
            })
        }
    });
    block_on_all(future).unwrap();
    output
}

错误输出:

error[E0599]: no method named `for_each` found for type `tiberius::stmt::QueryResult<tiberius::query::ResultSetStream<std::boxed::Box<dyn tiberius::BoxableIo>, tiberius::query::QueryStream<std::boxed::Box<dyn tiberius::BoxableIo>>>>` in the current scope
   --> src\main.rs:104:18
    |
104 |             rslt.for_each(|row| {
    |                  ^^^^^^^^
    |
    = note: the method `for_each` exists but the following trait bounds were not satisfied:
            `&mut tiberius::stmt::QueryResult<tiberius::query::ResultSetStream<std::boxed::Box<dyn tiberius::BoxableIo>, tiberius::query::QueryStream<std::boxed::Box<dyn tiberius::BoxableIo>>>> : futures_state_stream::StateStream`
            `&mut tiberius::stmt::QueryResult<tiberius::query::ResultSetStream<std::boxed::Box<dyn tiberius::BoxableIo>, tiberius::query::QueryStream<std::boxed::Box<dyn tiberius::BoxableIo>>>> : std::iter::Iterator`
    = 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:
    |
2   | use futures_state_stream::StateStream;
    |
sql-server rust
1个回答
0
投票

看起来future_state_stream发生了重大变化,我使用的是一个太新的版本,导致破坏。

我不得不将futures_state_stream版本降级到0.1.0,直到this issue得到解决。

© www.soinside.com 2019 - 2024. All rights reserved.