I can't seem to find a definitive answer for this, the question being, if I do a "cursor.execute('begin immediate')" statement before a select statement followed by a delete statement and then an insert statement and then the commit statement, will the database be locked before the select statement and all the way through to the commit statement?
The reason I want to do this is because I have a table that I basically rotate, taking the first record in the table, creating a couple variables from the result, and then taking that record, deleting it and then inserting it again as the last record in the table. The problem that I am having is that I run 4 scripts at the same time that all do the same thing and even though the table starts out looking something like this:
rowid 1, num 1
rowid 2, num 2
rowid 3, num 1
rowid 4, num 2
rowid 5, num 1
rowid 6, num 2
rowid 7, num 1
rowid 8, num 2
After some time, the table can end up looking like this:
rowid 250, num 2
rowid 251, num 1
rowid 252, num 1
rowid 253, num 2
rowid 254, num 2
rowid 255, num 2
rowid 256, num 1
rowid 257, num 1
I assume it ends up like that because sometimes a second script does a select right after the first script but before the first script has a chance to rotate the table.
Here's the script I have now:
##### get the next number from the db and rotate the table records
curs.execute('SELECT rowid, num FROM tbl_wheel ORDER BY rowid ASC LIMIT 1')
for row in curs:
rowid = row[0]
num = row[1]
query_rotate = 'DELETE FROM tbl_wheel WHERE rowid = ' + str(rowid)
curs.execute('begin')
curs.execute(query_rotate)
curs.execute('INSERT INTO tbl_wheel (num) VALUES (?)', str(num))
conn.commit()
I am wondering if I can fix the problem I'm having like this:
##### get the next number from the db and rotate the table records
curs.execute('begin immediate')
curs.execute('SELECT rowid, num FROM tbl_wheel ORDER BY rowid ASC LIMIT 1')
for row in curs:
rowid = row[0]
num = row[1]
query_rotate = 'DELETE FROM tbl_wheel WHERE rowid = ' + str(rowid)
curs.execute(query_rotate)
curs.execute('INSERT INTO tbl_wheel (num) VALUES (?)', str(num))
conn.commit()
EDIT: To clarify, the 4 scripts I have running that do the same thing, I should have said they each do mostly the same thing but a little bit different, depending on the variable it gets from the db. They then each do there own thing but then return to do exactly the same thing as each other, i.e. return to grab the next variable out of the db.