How to find a character in a string and replace it?

This is my sentence. I want to find each item and replace it:

UPDATE nucleic_test_institution SET institution_name = ? institution_address = ?WHERE id = ?

institution_name = ? and institution_address = ? replace institution_name = ?, and institution_address = ?

fn update_nucleic_institution(&self) -> bool {
        let mut add_symbols: Vec<String> = Vec::new();

        let mut sql_stmt = String::from("UPDATE nucleic_test_institution SET ");
        let mut coon = dbconfig::get_conn().unwrap();

        let data = self.into();

        match data {
            Some(data) => {
                match &data.institution_name {
                    Some(_institution_name) => {
                        sql_stmt.push_str("institution_name = ? ");
                        add_symbols.push(String::from("institution_name = ? "));
                    }
                    None => (),
                }
                match &data.institution_address {
                    Some(_institution_address) => {
                        sql_stmt.push_str("institution_address = ? ");
                        add_symbols.push(String::from("institution_address = ? "));
                    },
                    None => (),
                }
                match &data.institution_phone {
                    Some(_institution_phone) => {
                        sql_stmt.push_str("institution_phone = ? ");
                        add_symbols.push(String::from("institution_phone = ? "));
                    },
                    None => (),
                }
                match &data.institution_region {
                    Some(_institution_region) => {
                        sql_stmt.push_str("institution_region = ? ");
                        add_symbols.push(String::from("institution_region = ? "));
                    },
                    None => (),
                }
            }
            None => (),
        }
        sql_stmt.push_str(" WHERE id = ?");

        if add_symbols.len() > 1 {
            for item in add_symbols {

                let str = &item;
                let new_str = str.to_owned() + ",";

                println!("{}", new_str);

                // if sql_stmt.contains(&item) {
                //     sql_stmt.find(pat: P)
                // }
            }
        }
        println!("sql: {}", sql_stmt);

        let result = coon.exec_drop("UPDATE nucleic_test_institution SET institution_name = ?, institution_address = ?, institution_phone = ?, institution_region = ?
                                WHERE id = ?", 
                        (&self.institution_name, &self.institution_address, &self.institution_phone, &self.institution_region, &self.id));

        if result.is_ok(){
            return true;
        } 
        false
    }

I see find method will only return its Unicode value

From your code or seems to me that what you're looking for is something like the prepared statement feature offered by eg sqlx.

1 Like

yes, i'm writing an SQL parsing. I want to know how to remove the last element of the string array

     let mut add_symbols: Vec<String> = Vec::new();
     add_symbols.push(String::from("institution_name = ? "));
     add_symbols.push(String::from("institution_address = ? "));

        if add_symbols.len() > 1 {
            for mut item in add_symbols {
                item.push_str(",");
                sql_stmt.push_str(&item);
            }
        }

I want to remove institution_address = ?

have already been solved :smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.