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

问题描述

我正在尝试通过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,&mut pointer,null_mut()));
    println!("{}",sqlite3_bind_text(pointer,1,CString::new("hello".to_owned()).unwrap().as_ptr(),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 ?;",&stmt,NULL
) << endl;
cout << sqlite3_bind_text(stmt,"hello",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 ?;\0".as_ptr() as *const i8,-1,&mut pointer,null_mut()));
println!("{}",sqlite3_bind_text(pointer,1,b"google\0".as_ptr() as *const i8,SQLITE_TRANSIENT));

但是,我不知道为什么,大声笑?

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...