Android Question Adding database tables -UpdateDB2_3

Arf

Well-Known Member
Licensed User
Longtime User
I use SQL for a database of patients, the original database was created as follows:
B4X:
Sub UpdateDB1_2(sq As SQL)
    'empty database, lets create all required tables
    Dim m As Map
    m.Initialize 'clear the map
    m.Put("Patient_No", DBUtils.DB_TEXT)
    m.Put("First_Name", DBUtils.DB_TEXT)
    m.Put("Last_Name", DBUtils.DB_TEXT)
    m.Put("Date_of_Birth", DBUtils.DB_INTEGER)
    m.Put("Sex", DBUtils.DB_TEXT)
    m.Put("Patient_ID", DBUtils.DB_INTEGER)
    DBUtils.CreateTable(sq, "Patients", m, "") 'no primary key assigned
   
    m.Initialize 'clear the map
    m.Put("ExamTime", DBUtils.DB_INTEGER)
    m.Put("ExamPatient_ID", DBUtils.DB_INTEGER)
    m.Put("ExamTestNum", DBUtils.DB_INTEGER)
    m.Put("ExamData", DBUtils.DB_BLOB)
    DBUtils.CreateTable(sq, "Exams", m, "") 'no primary key assigned
    DBUtils.SetDBVersion(sq, 2)
End Sub

And now I need to add a height parameter to my stored patient data. So I can see how to create a similar UpdateDB2_3 function to do that, but I am not sure how to go about actually adding height to the map within that function. I don't want to delete my existing DDB and start over.

Would it be as simple as this, or will this damage my existing database?
B4X:
Sub UpdateDB2_3(sq As SQL)
    Dim m As Map
    m.Initialize 'clear the map
    m.Put("Patient_No", DBUtils.DB_TEXT)
    m.Put("First_Name", DBUtils.DB_TEXT)
    m.Put("Last_Name", DBUtils.DB_TEXT)
    m.Put("Date_of_Birth", DBUtils.DB_INTEGER)
    m.Put("Sex", DBUtils.DB_TEXT)
    m.Put("Patient_ID", DBUtils.DB_INTEGER)
   
    'now add height
    m.Put("Patient_Height", DBUtils.DB_INTEGER)
   
    DBUtils.CreateTable(sq, "Patients", m, "") 'no primary key assigned
    DBUtils.SetDBVersion(sq, 3)
End Sub
 

JTmartins

Active Member
Licensed User
Longtime User
If you create a table with the same name you will eventualy get an error..Actualy I never tried this. Anyway if you do not get an error a new table will be created and you will loose all data already inserted.

Your best option I think it will be execute a SQLite comand directly on the table as :

ALTERTABLE Patients ADDCOLUMN Patient_Height Integer
 
Upvote 0
Top