nextId function rewritten to use max(id) in sqlite