我需要使用tiberius收集表的所有行并输出它们。我的简单代码是:
extern crate futures;
extern crate tokio_core;
extern crate tiberius;
use futures::Future;
use tokio_core::reactor::Core;
use tiberius::SqlConnection;
use tiberius::stmt::ResultStreamExt;
fn main() {
let mut core = Core::new().unwrap();
let future = SqlConnection::connect(core.handle(), "server=tcp:127.0.0.1,1433;username=SA;password=qweasdZXC123!!;")
.and_then(|conn| {
let mut v: Vec<String> = Vec::new();
conn.simple_query("SELECT id, name FROM test").for_each_row(|row| {
let id: i32 = row.get(0);
let name: &str = row.get(1);
v.push(format!("{} - {}", id, name));
Ok(())
});
println!("{:?}", v);
Ok(())
});
core.run(future).unwrap();
}
此代码打印一个空向量,但我需要完整的字符串向量。我读过一些关于期货的文章,但作为 Rust 的新手,它们对我来说看起来太复杂了。