Python code to maintain an Ordered Table: ``` def OpenRowid(db, table, rowid, count=1): if count < 1: raise ValueError('Count must be >= 1') if db.cursor().execute(OpenRowid.test.format(table=table), (rowid, count)).fetchone()[0] != 0: db.cursor().execute(OpenRowid.sql.format(table=table), (rowid, count)) def RenumberTable(db, table): db.cursor().execute(RenumberTable.sql.format(table=table)) OpenRowid.test = ''' select count(*) from {table} where _rowid_ >= ?1 and _rowid_ < ?1 + ?2 ; ''' OpenRowid.sql = ''' insert into {table} select * from {table} where _rowid_ >= ? order by _rowid_ desc on conflict (_rowid_) do update set _rowid_ = excluded._rowid_ + ?; ''' RenumberTable.sql = ''' begin immediate; -- make sure we have the correct temporary table r drop table if exists temp.r ; create temporary table if not exists r ( old primary key, new ) without rowid ; -- for all _rowid_ > 0 compute new _rowid_ to close gaps insert into temp.r select _rowid_ as old, row_number() over (order by _rowid_) as new from {table} where _rowid_ > 0 order by _rowid_ ; -- apply _rowid_ updates in ascending order of _rowid_ (move _rowid_ down) insert into {table} select * from {table} where _rowid_ in ( select old from temp.r where old != new ) order by _rowid_ on conflict (_rowid_) do update set _rowid_ = ( select new from temp.r where old == excluded._rowid_ ) ; -- clear temporary table delete from temp.r; -- for _rowid_ > 0 compute offset _rowid_ so rows with _rowid_ < 1 can be moved into place insert into r select _rowid_ as old, row_number() over (order by _rowid_) + (select count(*) from {table} where _rowid_ < 1) as new from {table} where _rowid_ > 0 order by _rowid_ ; -- apply _rowid_ updates in descending order of _rowid_ (move _rowid_ up) insert into {table} select * from {table} where _rowid_ in ( select old from temp.r where old != new ) order by _rowid_ desc on conflict (_rowid_) do update set _rowid_ = ( select new from temp.r where old == excluded._rowid_ ) ; -- clear temporary table delete from temp.r; -- compute final _rowid_ for rows with _rowid_ < 1 insert into temp.r select _rowid_ as old, row_number() over (order by _rowid_) as new from {table} where _rowid_ < 1 order by _rowid_ ; -- apply _rowid_ updates in descending order of _rowid_ (move _rowid_ up) insert into {table} select * from {table} where _rowid_ in ( select old from temp.r where old != new ) order by _rowid_ desc on conflict (_rowid_) do update set _rowid_ = ( select new from temp.r where old == excluded._rowid_ ) ; -- clean up our temporary table drop table if exists temp.r; -- commit our transaction commit; ''' if __name__ == '__main__': import apsw db = apsw.Connection() db.cursor().execute('create table x(id integer primary key, v integer);') db.cursor().execute('insert into x select value*10, value from generate_series where start=1 and stop=10;') db.cursor().execute('insert into x select -(value-1)*10, -value from generate_series where start=1 and stop=10;') db.cursor().execute('delete from x where id=50;') db.cursor().execute('delete from x where id=-30;') for row in db.cursor().execute('select * from x;'): print(row) print() RenumberTable(db, 'x') for row in db.cursor().execute('select * from x;'): print(row) print() OpenRowid(db, 'x', 3) OpenRowid(db, 'x', -4) for row in db.cursor().execute('select * from x;'): print(row) print() db.cursor().execute('insert into x values (3, 103);') db.cursor().execute('insert into x values (-4, -104);') for row in db.cursor().execute('select * from x;'): print(row) print() RenumberTable(db, 'x') for row in db.cursor().execute('select * from x;'): print(row) print() ```