Best way to get the ID of the last inserted row on SQLite

前端 未结 4 1130
日久生厌
日久生厌 2020-12-14 01:40

On iPhone, what\'s the best way to get the ID of the last inserted row on an SQLite Database using FMDB ?

Is there a better way rather than doing :

S         


        
4条回答
  •  执念已碎
    2020-12-14 02:01

    For swift 4 use this code

    let lastRowId = sqlite3_last_insert_rowid(db)
    

    for example

    if sqlite3_exec(db, stringSql, nil, nil, nil) != SQLITE_OK {
        let errmsg = String(cString: sqlite3_errmsg(db)!)
        print("error Insert: \(errmsg)")
    }
    
    let lastRowId = sqlite3_last_insert_rowid(db);
    print(lastRowId) // its gives you last insert id
    
    if sqlite3_finalize(statement) != SQLITE_OK {
        let errmsg = String(cString: sqlite3_errmsg(db)!)
        print("error finalizing prepared statement: \(errmsg)")
    }
    statement = nil
    
    //*** close data base
    if sqlite3_close(db) != SQLITE_OK {
        print("error closing database")
    }
    db = nil
    

提交回复
热议问题