Merge data into a database

Verified
Added by iNTERFACEWARE

Basic example of merging data into a SQLite database (code is "plug and play" as the DB will be created if it does not exist)

Source Code
function main(Data)
   -- Parse the HL7 message
   local Msg, Name = hl7.parse{vmd = 'example/demo.vmd', data = Data}
   
   -- Create the database tables target node tree
   local Out       = db.tables{vmd = 'example/demo.vmd', name = Name}
   
   --Map (part of) the message
   Out.patient[1].Id        = Msg.PID[3][1][1]
   Out.patient[1].LastName  = Msg.PID[5][1][1][1]
   Out.patient[1].GivenName = Msg.PID[5][1][2]
   Out.patient[1].Ssn       = Msg.PID[19]
   
   -- (1) Save data to database
   
   -- connect to the database
   if not Conn or not Conn:check() then
      Conn = db.connect{
         api=db.SQLITE,
         name='test',
         user='',
         password='',
         live=true
      }
   end
   
   -- merge the data into the database
   Conn:merge{data=Out, live = true}
end
Description
Basic example of merging data into a SQLite database (code is "plug and play" as the DB will be created if it does not exist)
Usage Details

This code is a simple demonstration of merging data into a database. It does a very basic HL7 message mapping and then merges into a SQLite database. The code is “plug and play” because the SQLite database will be created if does not exist.

How to use the code:

  • Use a To Translator script
    • Add the code
    • Load the sample messages from SampleData.txt
  • Alternatively you can also load the attached project which already contains the sample messages
  • Inspect the code and annotations to see how it works

Note: This code is explained in detail in the Merging data into a Database tutorial.