blackbeam / rust-mysql-simple

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

Adaptation with mysql8.0 #299

Closed Peryol closed 2 years ago

Peryol commented 2 years ago

When I used exec to interact with mysql8.0, the parameter could not be written successfully. The code is as follows:

   `let url = "mysql://damn:123456@localhost:3306/menu";
    let opts = Opts::from_url(url).unwrap();
    let pool = Pool::new(opts).unwrap();
    let mut conn = pool.get_conn().unwrap();`

    let flag = conn.exec_batch(
     r"INSERT INTO customer (id, name, phone, sex, vip, time, secret)
     VALUES (:id, :name, :phoneNum, :sex, :vip, :time, :secret)",
    customer.iter().map(|p| params! {
        "id" => &p.id,
        "name" => &p.name,
        "phone" => &p.phoneNum,
        "sex" => &p.sex,
        "vip" => &p.vip,
        "time" => &p.time,
        "secret" => &p.secret,
        })
    );`

The error is as follows:

thread 'main' panicked at 'called' 'Result::unwrap()' on an 'Err' value: MySqlError { Error 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySql server version for the right syntax to use near '?Num, ?, ?, ?, ?) ' at line 2 } ', src/main.rs:46:7

After the error, I invited my friend gallon to check the syntax, parameter settings, and database files. Everything was correct. Finally, I renamed the variable'phoneNum'to'phone', and it worked well. At the same time, my friend gallon tested under MySQL that adding data to the'phoneNum' column was legal, so we finally thought it was a problem with the library, it seems that the library's adaptation to the hump naming method.

blackbeam commented 2 years ago

Oh. Seems like naming constrains wasn't mentioned in the documentation. Thanks for report.

Parser uses naming convention that is similar to a one, recommended by Rust. Namely - parameter must start with either _ or a..z and may continue with _, a..z and 0..9. I'll update the docs.

Peryol commented 2 years ago

Oh. Seems like naming constrains wasn't mentioned in the documentation. Thanks for report.

Parser uses naming convention that is similar to a one, recommended by Rust. Namely - parameter must start with either _ or a..z and may continue with _, a..z and 0..9. I'll update the docs.

OK, thank you for your answer

blackbeam commented 2 years ago

Naming convention is now mentioned in the docs (in the Named Parameters section), so closing.

austin230196 commented 2 years ago

When I used exec to interact with mysql8.0, the parameter could not be written successfully. The code is as follows:

   `let url = "mysql://damn:123456@localhost:3306/menu";
    let opts = Opts::from_url(url).unwrap();
    let pool = Pool::new(opts).unwrap();
    let mut conn = pool.get_conn().unwrap();`

    let flag = conn.exec_batch(
     r"INSERT INTO customer (id, name, phone, sex, vip, time, secret)
     VALUES (:id, :name, :phoneNum, :sex, :vip, :time, :secret)",
    customer.iter().map(|p| params! {
        "id" => &p.id,
        "name" => &p.name,
        "phone" => &p.phoneNum,
        "sex" => &p.sex,
        "vip" => &p.vip,
        "time" => &p.time,
        "secret" => &p.secret,
        })
    );`

The error is as follows:

thread 'main' panicked at 'called' 'Result::unwrap()' on an 'Err' value: MySqlError { Error 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySql server version for the right syntax to use near '?Num, ?, ?, ?, ?) ' at line 2 } ', src/main.rs:46:7

After the error, I invited my friend gallon to check the syntax, parameter settings, and database files. Everything was correct. Finally, I renamed the variable'phoneNum'to'phone', and it worked well. At the same time, my friend gallon tested under MySQL that adding data to the'phoneNum' column was legal, so we finally thought it was a problem with the library, it seems that the library's adaptation to the hump naming method.

Thanks alot I was stuck on this for along time I had a column in my users table called userId it kept on throwing errors I used different SQL syntaxes but nothing worked gave up for somedays but after seeing your update i changed it to user_id and it worked I'm very grateful to you 😊