From fc2ea5aa192d848b35667f13fb0c2063005f4945 Mon Sep 17 00:00:00 2001 From: Ivan Ukhov Date: Tue, 26 May 2020 20:19:53 +0200 Subject: [PATCH] Read the error message when sqlite3_open_v2 fails --- src/connection.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/connection.rs b/src/connection.rs index 0e6666d..b908fa4 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -28,25 +28,27 @@ impl Connection { pub fn open_with_flags>(path: T, flags: OpenFlags) -> Result { let mut raw = 0 as *mut _; unsafe { - let ret = ffi::sqlite3_open_v2( + let code = ffi::sqlite3_open_v2( path_to_cstr!(path.as_ref()).as_ptr(), &mut raw, flags.0, 0 as *const _, ); - - // Explicitly close the connection on error. - // This is a quirk of the C API, where the database handle - // (raw) is unconditionally allocated. - match ret { + match code { ffi::SQLITE_OK => {} - code => { - ffi::sqlite3_close(raw); - return Err(::Error { - code: Some(code as isize), - message: None, - }); - } + code => match ::last_error(raw) { + Some(error) => { + ffi::sqlite3_close(raw); + return Err(error); + } + _ => { + ffi::sqlite3_close(raw); + return Err(::Error { + code: Some(code as isize), + message: None, + }); + } + }, } } Ok(Connection {