Quantcast
Channel: VBForums - Database Development
Viewing all articles
Browse latest Browse all 2918

SQLite "Database is Locked" when moving a list of records from one table to another

$
0
0
I have a Public Sub to move a collection of records from one table to another in the same SQLite database. First it reads the first record from strFromTable, then writes it to strToTable, then deletes the record from strFromTable. To speed things up, I've loaded the entire collection of records into a transaction. When the list involves moving multiple image blobs (more than 3 or 4), the db gets backed up, and throws the exception "The Database is Locked". I think what is happening is that it's not finished writing one record before it starts trying to write the next record. Since SQLite only allows one write at a time, it thows the "Locked" exception.

Here is the code that triggers the error when moving multiple image blobs (I get no error when only moving 3 or 4):

Code:

    Using SQLconnect = New SQLiteConnection(strDbConnectionString)
        SQLconnect.Open()
        Using SQLcommand = SQLconnect.CreateCommand
            Using tr = SQLconnect.BeginTransaction()

                For Each itm As ListViewItem In lvcollection
                    SQLcommand.CommandText = $"INSERT INTO {strToTable} SELECT * FROM {strFromTable} WHERE id = {itm.Tag}; DELETE FROM {strFromTable} WHERE ID = {itm.Tag};"
                    SQLcommand.ExecuteNonQuery()
                Next

                tr.Commit()
            End Using
        End Using
    End Using

When I get rid of the transaction, it executes without error:

Code:

    Using SQLconnect = New SQLiteConnection(strDbConnectionString)
        SQLconnect.Open()
        Using SQLcommand = SQLconnect.CreateCommand

            For Each itm As ListViewItem In lvcollection
                SQLcommand.CommandText = $"INSERT INTO {strToTable} SELECT * FROM {strFromTable} WHERE id = {itm.Tag}; DELETE FROM {strFromTable} WHERE ID = {itm.Tag};"
                SQLcommand.ExecuteNonQuery()
            Next

        End Using
    End Using

I'm not very good with DB operations, so I'm sure there is something that needs improvement. Is there a way to make SQLite completely finish the previous INSERT before executing the next INSERT? How can I change my code to allow using a transaction?

Thank you for your help.

Viewing all articles
Browse latest Browse all 2918

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>