不使用结构体进行反序列化tokio_postgres行是可能的,可以使用tokio_postgres的Row
对象来访问行中的列数据。下面是一个示例代码:
use tokio_postgres::{Client, NoTls};
#[tokio::main]
async fn main() {
// 建立数据库连接
let (client, connection) = tokio_postgres::connect("host=localhost user=postgres", NoTls)
.await
.expect("Failed to connect to the database");
// 等待连接建立完成
if let Err(e) = connection.await {
eprintln!("Connection error: {}", e);
return;
}
// 执行查询语句
let rows = client
.query("SELECT id, name, age FROM users", &[])
.await
.expect("Failed to execute query");
// 遍历行
for row in rows {
// 使用列索引访问行中的数据
let id: i32 = row.get(0);
let name: &str = row.get(1);
let age: i32 = row.get(2);
println!("ID: {}, Name: {}, Age: {}", id, name, age);
}
}
在上述示例中,我们首先建立了与数据库的连接,然后执行了一条查询语句。查询结果中的每一行都表示为Row
对象。我们可以使用get
方法根据列的索引来获取行中的数据。请注意,列的索引是从0开始的。
在这个示例中,我们假设查询结果中有三列:id
(整数类型)、name
(字符串类型)和age
(整数类型)。我们通过get
方法将每列的值转换为对应的数据类型,并打印出来。
请根据实际情况修改查询语句和列的索引来适应你的应用程序。