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

MySQL - DriverError { Statement takes 4 parameters but 2 was supplied } #249

Closed
NielDuysters opened this issue Sep 22, 2020 · 4 comments
Closed

Comments

@NielDuysters
Copy link

I've got the following piece of code which compiles:

pub fn add_user(&self, data: RegistrationForm) -> bool {
            let mut conn = self.conn.get_conn().unwrap();

            let execute_result = conn.exec_batch("
            INSERT INTO users (
                username, email, birthdate, password
            ) VALUES (
                :username, :email, :birthdate, :password
            )", params! {
                "username" => &data.username,
                "email" => data.email,
                "birthdate" => data.birthdate,
                "password" => data.password
            });

            match execute_result {
                Ok(_) => {
                    println!("User {} added!", data.username);
                    true
                }
                Err(e) => {
                    eprintln!("Failed to add user {}: {}", data.username, e.to_string());
                    false
                }
            }
        }

I'm sure that data.username, data.email, data.birthdate, data.password ain't empty. For some reason I get the following error at runtime;

Failed to add user test: DriverError { Statement takes 4 parameters but 2 was supplied }

I guess this has something to do with the prepared statement failing. But I don't see how it could fail?

@blackbeam
Copy link
Owner

Thanks for report. I'll look into it.
But could you please try to wrap params! { .. } into vec![] and see if it still errors?

@blackbeam
Copy link
Owner

The purpose of exec_batch is to execute a statement repeatedly with different sets of parameters:

conn.exec_batch(query, vec![params1, params2, params3, ..]);

Turns out that the result of params! invocation itself satisfies the bound on exec_batch's second parameter (IntoIter<Item: Into<Params>>). So calls like the following one are possible, yet semantically incorrect:

conn.exec_batch(query, params! { .. });

Thanks for report. I'll fix this.

Regarding your code. Since there is only one set of parameters and the query result is ignored I suggest you use exec_drop.

@NielDuysters
Copy link
Author

Okay thanks. That solved it. Is there a way to get the insert id?

@NielDuysters
Copy link
Author

Found it.

self.conn.get_conn().unwrap().last_insert_id();

Thx

blackbeam added a commit to blackbeam/rust_mysql_common that referenced this issue Sep 28, 2020
xmas7 pushed a commit to RubyOnWorld/rust_mysql_common that referenced this issue Sep 6, 2022
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

2 participants