根据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程序员来说非常神秘!:()
我习惯用StdResult
和Option
以及Ok()
处理Err()
s和Some()
s,我觉得我需要在下面第7行做像Ok(rslt)
这样的事情,但这也给了我一个编译时错误。
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;
|
看起来future_state_stream发生了重大变化,我使用的是一个太新的版本,导致破坏。
我不得不将futures_state_stream版本降级到0.1.0,直到this issue得到解决。