Title says it all.. I have developed an app for my wife, but now she is wanting me to add more information to it. I didn't plan for future update/new information to be added, so my question is; How can I add new fields to an existing database without losing existing data? An online database would be the best way to do this (I think), but I have never done one of these.
I have the same problem. I use the following code:
B4X:
Sub AddColumns
If ColumnExist("MyTable", "NewColumn")=False Then
SQL1.ExecNonQuery("ALTER TABLE MyTable ADD NewColumn NVARCHAR(20)")
End If
If ColumnExist(..........
.......
End If
End Sub
Sub ColumnExist(Table As String, ColName As String) As Boolean
Dim Cursor1 As Cursor
Dim Query As String
Query="SELECT * FROM " & Table & " LIMIT 1"
Cursor1 = SQL1.ExecQuery(Query)
For i=0 To Cursor1.ColumnCount - 1
Cursor1.Position = 0
If Cursor1.GetColumnName(i)= ColName Then
Cursor1.close
Return True
End If
Next
Cursor1.close
Return False
End Sub
Since you know the old database's columns' number, you can just read the columnCount and if it's less than the new one's, alter your table by adding the new fields.