Here's a little sub to backup and alter a table (assuming you have all the SQlite parts in the starter service).
The sub checks if the column is already defined. Of course you can leave the loop at once with a return. For debug reasons I've used a Boolean here.
The sub checks if the column is already defined. Of course you can leave the loop at once with a return. For debug reasons I've used a Boolean here.
B4X:
Sub AddColumnToTable (TableName As String, CToAdd As String, CFormat As String)
Private Row As Int
Private Query As String
' Dim TableName As String = "items" 'tablename
' Dim CToAdd As String = "iid" 'column to add
' Dim CFormat As String = "INTEGER" 'format of the column
Dim DBDir As String =Starter.SQLDataBasePath
Dim DBName As String =Starter.SQLDateBaseName
Query = "SELECT * FROM " & TableName 'get all columns
Cursor1 = Starter.SQL1.ExecQuery(Query)
Dim Found As Boolean = False
For l=0 To Cursor1.ColumnCount-1
Log(Cursor1.GetColumnName(l))
If Cursor1.GetColumnName(l) = CToAdd Then
Found=True
Exit
End If
Next
If Found=False Then 'backup before alter ;-)
File.Copy(DBDir,DBName,DBDir,DBName & DateTime.Now & "_backup") 'backup before altering table
Query = "ALTER TABLE " & TableName & " ADD COLUMN " & CToAdd & " " & CFormat
Starter.SQL1.ExecNonQuery(Query)
End If
Query = "PRAGMA table_info(" & TableName &")" 'Get table info -> shows column names and format
Cursor1 = Starter.SQL1.ExecQuery(Query)
For Row = 0 To Cursor1.RowCount - 1
Cursor1.Position = Row
Log(Cursor1.GetString2(1) & ":" & Cursor1.GetString2(2))
Next
End Sub