pub trait ColumnCapabilities<'a> {
// Required methods
fn get_data(
stmt: *mut sqlite3_stmt,
i: usize,
) -> Result<Self, MinSqliteWrapperError<'a>>
where Self: Sized;
fn bind_val(self, stmt: *mut sqlite3_stmt, i: usize) -> SqlitePrimaryResult
where Self: Sized;
}Expand description
Defines the helper functions that work on the columns of the data rows received.
Required Methods§
Sourcefn get_data(
stmt: *mut sqlite3_stmt,
i: usize,
) -> Result<Self, MinSqliteWrapperError<'a>>where
Self: Sized,
fn get_data(
stmt: *mut sqlite3_stmt,
i: usize,
) -> Result<Self, MinSqliteWrapperError<'a>>where
Self: Sized,
Reads the column data of the rows that returns from the SQL query.
§Panics
- If the data type is incorrectly specified.
- If the column index doesn’t match.
§Usage
#[derive(Debug)]
struct Item {
id: i64,
name: String,
tag: String,
}
let db_path = Path::new("./example.db");
let db = Database::open(db_path).unwrap();
let statement = String::from(
"SELECT * FROM example_table WHERE ID = '15';"
);
let mut sql = db.prepare(statement, None::<Box<dyn FnOnce(SqlitePrimaryResult, String)>>).unwrap();
while let PreparedStatementStatus::FoundRow = sql.execute_prepared() {
println!(
"id = {}, name = {}, tag = {}",
sql.get_data::<i64>(0).unwrap(),
sql.get_data::<String>(1).unwrap(),
sql.get_data::<String>(2).unwrap(),
);
// OR
println!(
"{:?}",
Item {
id: sql.get_data(0).unwrap(),
name: sql.get_data(1).unwrap(),
tag: sql.get_data(2).unwrap(),
}
);
}
sql.kill();
db.close();Sourcefn bind_val(self, stmt: *mut sqlite3_stmt, i: usize) -> SqlitePrimaryResultwhere
Self: Sized,
fn bind_val(self, stmt: *mut sqlite3_stmt, i: usize) -> SqlitePrimaryResultwhere
Self: Sized,
Binds the value of a parameter to a prepared statement indicator.
Supported indicator patterns:
- ?
- ?NNN
- :VVV
- @VVV
- $VVV
Returns SqlitePrimaryResult:Ok on success or an error code if anything goes wrong.
SqlitePrimaryResult::Range is returned if the parameter index is out of range.
§IMPORTANT
The first argument isn’t index of the column. It’s simply index of the
indicator and always starts at 1. If the first argument is given zero,
the function will return SqlitePrimaryResult::Range.
§Usage
let db_path = Path::new("./example.db");
let db = Database::open(db_path).unwrap();
let statement = String::from(
"SELECT * FROM example_table WHERE ID = ;"
);
let mut sql = db.prepare(statement, None::<Box<dyn FnOnce(SqlitePrimaryResult, String)>>).unwrap();
let status = sql.bind_val(1, 5);
// You can do some checks by
assert_eq!(status, SqlitePrimaryResult::Ok);
// or
if status == SqlitePrimaryResult::Range {
panic!("Out of index on sql.bind_val!");
}
sql.kill();
db.close();