根据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板条箱版本0.3.2的方式如下,并且出现编译时错误。我无法判断自己在做什么错,因为QueryResult
类型并不表示我需要使用哪种包装器来包装值(QueryResult文档中没有手动编写的文档或示例,只有自动生成的API文档,对于新的Rust程序员来说是非常隐秘的!:()
我习惯使用StdResult
和Option
和Ok()
之类的东西来处理Err()
和Some()
,我觉得我需要在下面的第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;
|
答案 0 :(得分:0)
好像对futures_state_stream进行了重大更改,并且我为此软件包使用了太新版本,这导致了损坏。
在解决this issue之前,我不得不将futures_state_stream版本降级到0.1.0。