Skip to content

Instantly share code, notes, and snippets.

@veer66
Created January 26, 2019 11:27
Show Gist options
  • Save veer66/6338d018dbef525cc3e2cc8dfbb01014 to your computer and use it in GitHub Desktop.
Save veer66/6338d018dbef525cc3e2cc8dfbb01014 to your computer and use it in GitHub Desktop.
I tried to get last insert rowid from SQLite via Diesel. #rust
#[macro_use]
extern crate diesel;
pub mod models;
pub mod schema;
use diesel::prelude::*;
use diesel::sqlite::SqliteConnection;
use self::models::{Post, NewPost};
pub fn create_post<'a>(conn: &SqliteConnection, title: &'a str, body: &'a str) -> usize {
use schema::posts;
let new_post = NewPost {
title: title,
body: body,
};
diesel::insert_into(posts::table)
.values(&new_post)
.execute(conn)
.expect("Cannot insert")
}
pub fn establish_connection() -> SqliteConnection {
let database_url = "tutu.db";
SqliteConnection::establish(&database_url).expect("Can't connect DB")
}
no_arg_sql_function!(last_insert_rowid, diesel::sql_types::Bigint);
fn main() {
let connection = establish_connection();
create_post(&connection, "title", "body");
let x: i64 = diesel::select(last_insert_rowid).first(&connection).unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment