blackbeam / rust-mysql-simple

Mysql client library implemented in rust.
Apache License 2.0
658 stars 144 forks source link

how to query a table with over 20 columns #294

Closed guchengxi1994 closed 2 years ago

guchengxi1994 commented 2 years ago

I was using this tool in this situation, but it raise this exception.

the trait bound `(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _): FromValue` is not satisfied
required because of the requirements on the impl of `FromRow` for `(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _)`
blackbeam commented 2 years ago

Hi. Please use the Row type as a target type for a mysql row.

Example:

let query = "SELECT 1 as the_first_column, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20";
let row: mysql::Row = query.first(&mut conn)?.unwrap();

// take the first column value by the column name
let the_fist_column: u8 = row.take("the_fist_column").unwrap();

// take the last column value by the column index
let the_last_column: u8 = row.take(19).unwrap();

assert_eq!(the_fist_column, 1);
assert_eq!(the_last_column, 20);

Another option is to use frunk::Hlist type as a target type and then to use into_tuple2 to get columns values.