pub struct Cursor<'s, 'a: 's, 'b: 's, S: 's, AC: AutocommitMode> { /* private fields */ }
Expand description
Used to retrieve data from the fields of a query result
Implementations§
Source§impl<'a, 'b, 'c, S, AC: AutocommitMode> Cursor<'a, 'b, 'c, S, AC>
impl<'a, 'b, 'c, S, AC: AutocommitMode> Cursor<'a, 'b, 'c, S, AC>
Sourcepub fn get_data<'d, T>(&'d mut self, col_or_param_num: u16) -> Result<Option<T>>where
T: Output<'d>,
pub fn get_data<'d, T>(&'d mut self, col_or_param_num: u16) -> Result<Option<T>>where
T: Output<'d>,
Retrieves data for a single column in the result set
§Panics
If you try to convert to &str
but the data can’t be converted
without allocating an intermediate buffer.
Examples found in repository?
examples/list_tables.rs (line 38)
24fn list_tables(conn: &Connection<AutocommitOn>) -> Result<(), Box<dyn Error>> {
25 let stmt = Statement::with_parent(conn)?;
26 // Create the table
27 let mut table = Table::new();
28 // Add a row per time
29 table.add_row(row!["CATALOG_NAME","SCHEMA_NAME", "TABLE_NAME","TYPE"]);
30 // Print the table to stdout
31 table.printstd();
32
33 let mut rs = stmt.tables_str("%", "%", "%", "TABLE")?;
34 let cols = rs.num_result_cols()?;
35 while let Some(mut cursor) = rs.fetch()? {
36
37 for i in 1..(cols + 1) {
38 match cursor.get_data::<&str>(i as u16)? {
39 Some(val) => print!(" {},", val),
40 None => print!(" NULL,"),
41 }
42 }
43 //table.add_row(row![val_temp[1],val_temp[2],val_temp[3],val_temp[4],val_temp[5]]);
44 println!();
45 }
46 Ok(())
47}
More examples
examples/execute_query.rs (line 39)
27fn execute_statement(conn: &Connection<AutocommitOn>) -> Result<(), Box<dyn Error>> {
28 let stmt = Statement::with_parent(conn)?;
29
30 let mut sql_text = String::new();
31 println!("Please enter SQL statement string: ");
32 io::stdin().read_line(&mut sql_text).unwrap();
33
34 match stmt.exec_direct(&sql_text)? {
35 Data(mut stmt) => {
36 let cols = stmt.num_result_cols()?;
37 while let Some(mut cursor) = stmt.fetch()? {
38 for i in 1..(cols + 1) {
39 match cursor.get_data::<&str>(i as u16)? {
40 Some(val) => print!(" {}", val),
41 None => print!(" NULL"),
42 }
43 }
44 println!();
45 }
46 }
47 NoData(_) => println!("Query executed, no data returned"),
48 }
49
50 Ok(())
51}
examples/connect.rs (line 42)
30fn execute_statement<'env>(conn: &Connection<'env, AutocommitOn>) -> Result<(),Box<dyn Error>> {
31 let stmt = Statement::with_parent(conn)?;
32
33 let mut sql_text = String::new();
34 println!("Please enter SQL statement string: ");
35 io::stdin().read_line(&mut sql_text).unwrap();
36
37 match stmt.exec_direct(&sql_text)? {
38 Data(mut stmt) => {
39 let cols = stmt.num_result_cols()?;
40 while let Some(mut cursor) = stmt.fetch()? {
41 for i in 1..(cols + 1) {
42 match cursor.get_data::<&str>(i as u16)? {
43 Some(val) => print!(" {}", val),
44 None => print!(" NULL"),
45 }
46 }
47 println!();
48 }
49 }
50 NoData(_) => println!("Query executed, no data returned"),
51 }
52
53 Ok(())
54}
examples/connect_pool.rs (line 19)
6fn main() {
7 let manager = ODBCConnectionManager::new("DSN=dashdb;DATABASE=FOO;hostname=test@test.com;PORT=0000;UID=admin;PWD=admin;");
8 let pool = r2d2::Pool::new(manager).unwrap();
9
10 let mut children = vec![];
11 for i in 0..10i32 {
12 let pool = pool.clone();
13 let pool_conn = pool.get().unwrap();
14 children.push(thread::spawn(move || {
15 let conn = pool_conn.raw();
16 let stmt = Statement::with_parent(&conn).unwrap();
17 if let Data(mut stmt) = stmt.exec_direct("select * from dbtest.GLWTACT").unwrap() {
18 while let Some(mut cursor) = stmt.fetch().unwrap() {
19 if let Some(val) = cursor.get_data::<&str>(1).unwrap() {
20 println!("THREAD {} {}", i, val);
21 }
22 }
23 }
24 }));
25 }
26
27 for child in children {
28 let _ = child.join();
29 }
30}
examples/bind_params.rs (line 24)
8fn test_me() -> std::result::Result<(), Box<dyn Error>> {
9 let env = create_environment_v3().expect("Can't create ODBC environment");
10 let conn = env.connect("dashdb", "admin", "admin")?;
11 let stmt = Statement::with_parent(&conn)?.prepare(
12 "select * from test where COL3 = ?",
13 )?;
14
15 let param = "Binit";
16
17 let stmt = stmt.bind_parameter(1, ¶m)?;
18 /*let stmt = stmt.bind_parameter(2, ¶m)?;
19 let stmt = stmt.bind_parameter(3, ¶m)?;
20 let stmt = stmt.bind_parameter(4, ¶m)?;*/
21
22 let stmt = if let Data(mut stmt) = stmt.execute()? {
23 if let Some(mut cursor) = stmt.fetch()? {
24 println!("{}", cursor.get_data::<String>(1)?.unwrap());
25 }
26 stmt.close_cursor()?
27 } else {
28 panic!("SELECT statement returned no result set");
29 };
30
31 let stmt = stmt.reset_parameters()?;
32
33 let param = 128u8;
34
35 let stmt = stmt.bind_parameter(1, ¶m)?;
36 let stmt = stmt.bind_parameter(2, ¶m)?;
37
38 let stmt = if let Data(mut stmt) = stmt.execute()? {
39 if let Some(mut cursor) = stmt.fetch()? {
40 println!("{}", cursor.get_data::<String>(1)?.unwrap());
41 }
42 stmt.close_cursor()?
43 } else {
44 panic!("SELECT statement returned no result set");
45 };
46 stmt.reset_parameters().unwrap();
47
48 Ok(())
49}
Auto Trait Implementations§
impl<'s, 'a, 'b, S, AC> Freeze for Cursor<'s, 'a, 'b, S, AC>
impl<'s, 'a, 'b, S, AC> RefUnwindSafe for Cursor<'s, 'a, 'b, S, AC>where
S: RefUnwindSafe,
AC: RefUnwindSafe,
impl<'s, 'a, 'b, S, AC> !Send for Cursor<'s, 'a, 'b, S, AC>
impl<'s, 'a, 'b, S, AC> !Sync for Cursor<'s, 'a, 'b, S, AC>
impl<'s, 'a, 'b, S, AC> Unpin for Cursor<'s, 'a, 'b, S, AC>
impl<'s, 'a, 'b, S, AC> !UnwindSafe for Cursor<'s, 'a, 'b, S, AC>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more