Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use the column_count method of Statement? #376

Open
gmosx opened this issue Aug 27, 2024 · 2 comments
Open

How to use the column_count method of Statement? #376

gmosx opened this issue Aug 27, 2024 · 2 comments

Comments

@gmosx
Copy link

gmosx commented Aug 27, 2024

I am wondering, how is the column_count method supposed to be used.

AFAICU, you have to prepare and execute (query) the Statement, before you can call this method:

let mut stmt = conn.prepare(sql)?;
let mut rows = stmt.query([])?;
let column_count = stmt.column_count();

// IMPORTANT:
while let Some(row) = rows.next().unwrap() {
    ...
}

The problem is that the query* methods take a mutable reference to the Statement. When trying to call the column_count method, the borrow-checker complains:

cannot borrow `stmt` as immutable because it is also borrowed as mutable

Please note that the error is only triggered if the rows variable is actually used!

I would like to use the column count when 'deserializing' the rows. Any help/hints will be appreciated.

@gmosx gmosx changed the title How to use column_count method of Statement. How to use the column_count method of Statement? Aug 27, 2024
@gmosx
Copy link
Author

gmosx commented Aug 30, 2024

Well, one solution is to 'collect' the rows variable and eliminate the ref to the Statement:

let records = rows.map(|r| ...);
let records = records.collect::<Vec<....>>().unwrap();

But this prohibits using the column_count in the 'deserialization code'.

@gmosx
Copy link
Author

gmosx commented Aug 30, 2024

Actually you can use:

let col_count = rows.as_ref().unwrap().column_count();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant