B4J Question db-wal and db-shm files

stevetheframe

Member
Licensed User
Longtime User
I am developing a program with an sqlite database. I have noticed when I import a fresh database file there is often a "Mal-formed database" error in the logs.
It seems I can cure this by deleting the db-wal and db-shm files (in the same folder as the database file).
Am I safe to code for this in the final program (code below)?

Import Database:
Dim f As String = fc.ShowOpen(MainForm)
                If f <> "" Then
                    
                    If File.Exists(File.DirData("PPAServer"),"WPP6.db-wal") Then
                        File.Delete(File.DirData("PPAServer"),"WPP6.db-wal")
                    End If
                    If File.Exists(File.DirData("PPAServer"),"WPP6.db-shm") Then
                        File.Delete(File.DirData("PPAServer"),"WPP6.db-shm")
                    End If
                    File.Copy(fc.InitialDirectory,"/wpp6.db",File.DirData("PPAServer"), "wpp6.db")
                    fx.Msgbox(MainForm,"Fresh Database Imported." & CRLF & "Restart Server.", "Done")       
                End If   
            Catch
                fx.Msgbox(MainForm,"Error Importing Database!", "Oops")
                Log(LastException)
            End Try
 

KMatle

Expert
Licensed User
Longtime User
Usually these files will be deleted automatically when the db is closed properly. These files are some sort of buffering data when you use it with multiple instances accessing the db.

If you have only one instance, deactivate the wal function (browse for it to see how it's done).
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
I have noticed when I import a fresh database file there is often a "Mal-formed database" error in the logs.
You can open the database or in single user mode or in multi user "WAL" or "Write-Ahead Logging" mode. You have to take a choice and you can mix both database open modes.

It looks to me that you import a database which was in-use or open multi users mode. That means that not all mutations are write to the database file itself. That will be automatically done after all users have close their database connection by the checkpoint function and if the OS has flush the file buffer after the contents is write to the database. Deleting the *.shm wal file, makes your database corrupts which is displayed in the log.
 
Upvote 0

stevetheframe

Member
Licensed User
Longtime User
The files are not deleted when the database is closed and these files are part of the database when the journal mode is set to WAL.
Thanks for all the info.
It would seem that I am correct to check for and delete these files when importing a fresh sqlite database file.
 
Upvote 0
Top