Help! struct to connect sqlite and insert data

Hi, i testing using diesel and sqlite, and instantiate a struct. :face_with_spiral_eyes:
but this piece not work for me.

struct Database {
    connection: SqliteConnection,
}

impl Database {
    pub fn new() -> Result<Self, diesel::result::ConnectionError> {
        dotenv().ok();
        let database_url = env::var("DATABASE_URL")
                                .expect("DATABASE_URL in .env");
        let connection = SqliteConnection::establish(&database_url)
                                .expect("Error connecting DB");
        //Database {connection}
        Ok(Self {connection})
    }
    
    pub fn connection(&self) -> &SqliteConnection {
        &self.connection
    }
   
} // end impl Database

    use crate::movies::dsl::movies;
    let mut data = match Database::new() {        
        Ok(db) => { println!("ok open conection");                     
                    let new_movie = MovieInsert {   id: 0,
                                                    title: "aaa".to_string(),
                                                    year: 1999,
                                                    country: "bbbb".to_string(),
                    };

                    diesel::insert_into(movies)
                        .values(&new_movie)                        
                        .execute(&db)
                        .expect("Error insert book");
        
        },
        Err(e) => { eprintln!("Failed to connect: {}", e);
                    return;            
        }
    };

error[E0308]: mismatched types
--> src/main.rs:1606:34
|
1606 | .execute(&db)
| ------- ^^^ types differ in mutability
| |
| arguments to this method are incorrect

The execute method expects a mutable reference to your database connection.

1 Like

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.