dimanche 10 mai 2015

SQLITE delete and update in loop not working as expected.

The code below will not completely delete all rows from the SQLite database and update it as expected.

Input is a two dimensional array with all indexes assigned a value. During the first loop all rows of tbl_one are deleted, proceeded by the insertion of a new row, and then insertion of more rows as the loop progresses.

a = ['1;Value', '2;Value', '3;Value']

for i, value in enumerate(a):

   if i == 0:

      internaldatabase(self).execute('''Delete from tbl_one''')

   a[i] = list(value.split(";"))

   internaldatabase(self).execute('''INSERT INTO tbl_one(collum1, collum2) VALUES (?,?)''', (a[i][0], a[i][1]))

If all rows are selected then output would be:

1 Value 2 Value 3 Value 1 Value 2 Value 1 Value

Where as I would expect it to be:

1 Value 2 Value 3 Value

In the function internaldatabase the isolation_level is set to None, indicating implicit commits.

def internaldatabase(self):

    p = sys.path[0]+'''resource path'''

    p = sqlite3.connect(p, isolation_level=None)

    p = p.cursor()

    return(p)

I have already tried commit() and deleting/recreating the database, both with the same results.

Aucun commentaire:

Enregistrer un commentaire