Check sqlite db table columns
Posted: Sun Sep 19, 2021 2:08 pm
Not really sure if my question belongs here. I have a sqlite database and every now and then I make changes to the database, I add or alter a table’s column. So, I need to check that a user has the latest db schema. I have the following code that tries to do it but it is not working. It cannot delete the temp table and “database table is locked” error returns. Any suggestions?
Now, I get the followin errors:
Code: Select all
Enumeration
#db
EndEnumeration
Procedure CheckDatabaseUpdate(Database, Query$)
Result = DatabaseUpdate(Database, Query$)
If Result = 0
MessageRequester("Error",DatabaseError(),#MB_ICONERROR)
EndIf
ProcedureReturn Result
EndProcedure
Procedure CheckDB()
CheckDatabaseQuery(#db,"SELECT sql FROM sqlite_master WHERE tbl_name = 'Students' AND type = 'table'")
NextDatabaseRow(#db)
Debug GetDatabaseString(#db,0)
If GetDatabaseString(#db,0) <> "CREATE TABLE Students (id CHAR(15) PRIMARY KEY, Lname CHAR(32), Fname CHAR(32), Class CHAR(4))"
CheckDatabaseUpdate(#db,"PRAGMA foreign_keys = 0")
CheckDatabaseUpdate(#db,"BEGIN TRANSACTION")
CheckDatabaseUpdate(#db,"ALTER TABLE Lessons RENAME To sqlitemanager_temp_table_177354083607")
CheckDatabaseUpdate(#db,"CREATE TABLE Lessons (id CHAR(13), LName CHAR(30), Class CHAR(4))")
CheckDatabaseUpdate(#db,"INSERT INTO Lessons (id,LName,Class) Select id,LName,Class FROM sqlitemanager_temp_table_177354083607")
CheckDatabaseUpdate(#db,"DROP TABLE sqlitemanager_temp_table_177354083607")
CheckDatabaseUpdate(#db,"COMMIT")
CheckDatabaseUpdate(#db,"PRAGMA foreign_keys = 1")
EndIf
FinishDatabaseQuery(#db)
EndProcedure
If OpenDatabase(#db, DatabaseFile$, "", "", #PB_Database_SQLite)
CheckDB()
EndIf
there is already another table or index with this name: sqlitemanager_temp_table_177354083607
table Lessons already exists
database table is locked