On Fri, 31 Aug 2007 12:28:49 +0530, "B V, Phanisekhar" <[EMAIL PROTECTED]> wrote:
> Hi Gerhard, > > I am finding your code really tough to understand. Can you > please provide some comments? try: from pysqlite2 import dbapi2 as sqlite except ImportError: import sqlite3 as sqlite def init_tables(con): """ This function has to be run immediately after schema creation. It fills the internal SQLite table sqlite_sequence. This is necessary because SQLite creates entries for the sequences only on first use of the sequence, but we don't want to use the sequence via autoincrement fields, but using our own function that gets explicit id ranges. con: connection object """ con.execute(""" insert into sqlite_sequence(name, seq) select name, 1 from sqlite_master where type='table' and name not like 'sqlite%' """) def get_id_range(con, table, n): """ Retrieves a tuple with an id range that can be used for the primary key of the table `table`. con: connection object table: name of the table to get the id range for n: number of usable ids to be allocated """ isolation_level = con.isolation_level start, end = None, None try: con.isolation_level = None # autocommit mode con.execute("BEGIN EXCLUSIVE") start = con.execute("SELECT SEQ FROM SQLITE_SEQUENCE WHERE NAME=?", (table,)).fetchone()[0] end = start + n - 1 con.execute("UPDATE SQLITE_SEQUENCE SET SEQ=? WHERE NAME=?", (end, table)) con.execute("COMMIT") finally: con.isolation_level = isolation_level return start, end if __name__ == "__main__": # Test code, manually look wether the output makes sense ;-) con = sqlite.connect(":memory:") con.execute("create table test(id integer primary key autoincrement, name text)") init_tables(con) print get_id_range(con, "test", 1000) print get_id_range(con, "test", 1000) print get_id_range(con, "test", 1000) con.execute("insert into test(name) values ('foo')") con.execute("insert into test(name) values ('foo')") con.execute("insert into test(name) values ('foo')") ----------------------------------------------------------------------------- To unsubscribe, send email to [EMAIL PROTECTED] -----------------------------------------------------------------------------