问题描述
我正在尝试通过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));
但是,我不知道为什么,大声笑?