File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -64,6 +64,7 @@ public protocol SQLiteType {
6464 func getRow( sql: String , valuesToBind: SQLValues ? , valuesToGet: SQLValues ) throws -> [ SQLValues ]
6565 func getAllRows( in tableName: String , valuesToGet: SQLValues ) throws -> [ SQLValues ]
6666 func getByID( in tableName: String , id: Int , valuesToGet: SQLValues ) throws -> SQLValues
67+ func getLastInsertID( ) throws -> Int
6768 func vacuum( ) throws
6869 func resetAutoincrement( in tableName: String ) throws
6970 func query( sql: String , valuesToBind: SQLValues ? ) throws
@@ -417,6 +418,22 @@ open class SQLite: SQLiteType {
417418 }
418419 }
419420
421+ public func getLastInsertID( ) throws -> Int {
422+ let sqlStatement = try prepareStatement ( sql: " SELECT last_insert_rowid(); " )
423+ defer {
424+ sqlite3_finalize ( sqlStatement)
425+ }
426+
427+ try bindPlaceholders ( sqlStatement: sqlStatement, valuesToBind: nil )
428+
429+ guard sqlite3_step ( sqlStatement) == SQLITE_ROW else {
430+ throw SQLiteError . Step ( getErrorMessage ( dbPointer: dbPointer) )
431+ }
432+ let id = sqlite3_column_int ( sqlStatement, 0 )
433+ log ( " successfully got last_insert_id: \( id) " )
434+ return Int ( id)
435+ }
436+
420437 /// Repack the DB to take advantage of deleted data
421438 public func vacuum( ) throws {
422439 let sql = " VACUUM; "
You can’t perform that action at this time.
0 commit comments