< 返回版块

子十 发表于 2021-12-13 10:17

Tags:rbatis,pysql

rbatis py_sql 写了两个示例:

#[crud_table]
#[derive(Clone, Debug)]
pub struct Posts {
    pub id: Option<i32>,
    pub title: Option<String>,
    pub body: Option<String>,
    pub created_at: Option<rbatis::DateTimeNative>,
}

/// 这个没有参数可以成功执行
#[py_sql("select * from posts")]
async fn find_posts(rb: Arc<Rbatis>) -> Result<Vec<Posts>, Error> { todo!() }

/// 这个传入 id 参数就报错
#[py_sql("select * from posts where id=#{id} limit 1")]
async fn find_one(rb: Arc<Rbatis>, id: i32) -> Option<Posts> { todo!() }

错误如下:

error[E0308]: mismatched types
  --> src/bin/rb.rs:67:1
   |
67 | #[py_sql("select * from posts where id=#{id} limit 1")]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected reference, found `i32`
   |
   = note: expected reference `&_`
                   found type `i32`
   = note: this error originates in the attribute macro `py_sql` (in Nightly builds, run with -Z macro-backtrace for more info)

麻烦大佬指导下 _

评论区

写评论
作者 子十 2021-12-13 11:06

这个不行一样的错

--
👇
苦瓜小仔: 试试 #[py_sql("select * from posts where id=#{&id} limit 1")]

作者 子十 2021-12-13 11:05

感谢 wx 群的 洋仔 改成 &i32 可以了 如下:

#[py_sql("select * from posts where id=#{id} limit 1")]
async fn find_one(rb: Arc<Rbatis>, id: &i32) -> Option<Posts> { todo!() }

/// 调用方式
#[tokio::main]
async fn main() {
    let id = 7;
    let res = find_one(rb.clone(), &id).await;

    println!("{:?}", res);
}
苦瓜小仔 2021-12-13 11:03

试试 #[py_sql("select * from posts where id=#{&id} limit 1")]

1 共 3 条评论, 1 页