如何在 Rust 中将绑定传递给 sqlite3?

How do I pass bindings to sqlite3 in Rust?

我正在尝试通过 FFI 将绑定传递给 sqlite3。 sqlite3 被单独编译成 sqlite3.dll 并被 #[link(name = "sqlite3")]

加载

ext.rs

pub fn sqlite3_prepare_v3(db: *mut sqlite3, zSql: *const c_char, nByte: c_int,
                              prepFlags: c_uint, ppStmt: *mut *mut sqlite3_stmt, pzTail: *mut *const c_char) -> c_int;
pub fn sqlite3_bind_text(stmt: *mut sqlite3_stmt, w: c_int, b: *const c_char, n: c_int,
                             f: Option<c_int>) -> c_int;
pub fn sqlite3_step(stmt: *mut sqlite3_stmt) -> c_int;
pub fn sqlite3_column_text(stmt: *mut sqlite3_stmt, iCol: c_int) -> *const c_uchar;

main.rs

unsafe {
    let mut pointer = null_mut();
    println!("{}", sqlite3_prepare_v3(self.sqlite, CString::new("SELECT ?;".to_owned()).unwrap().as_ptr(), -1, 0, &mut pointer, null_mut()));
    println!("{}", sqlite3_bind_text(pointer, 1, CString::new("hello".to_owned()).unwrap().as_ptr(), -1, Some(SQLITE_TRANSIENT)));
    println!("{}", sqlite3_step(pointer));
    println!("{}", CStr::from_ptr(sqlite3_column_text(pointer, 0) as *const i8).to_str().unwrap());
}

结果是:

0
0
100
 <- Note it's empty here, and how do I get 'hello' instead?

当我在 C++ 上尝试这个时,像这样:

sqlite3_stmt* stmt;
cout << sqlite3_prepare_v3(
    ppdb,
    "SELECT ?;",
    -1,
    0,
    &stmt,
    NULL
) << endl;
cout << sqlite3_bind_text(stmt, 1, "hello", -1, SQLITE_TRANSIENT) << endl;
cout << sqlite3_step(stmt) << endl;
cout << sqlite3_column_text(stmt, 0);

我明白了

0
0
100
hello

我用

解决了这个问题
println!("{}", sqlite3_prepare_v3(self.sqlite, b"SELECT ?;[=10=]".as_ptr() as *const i8, -1, 0, &mut pointer, null_mut()));
println!("{}", sqlite3_bind_text(pointer, 1, b"google[=10=]".as_ptr() as *const i8, -1, SQLITE_TRANSIENT));

不过我也不知道为什么,哈哈