Update a database table

Verified
Added by iNTERFACEWARE

How to update a database using data from an HL7 message (similar principles apply to other message types)

Source Code
function main(Data)
   -- Parse the HL7 message
   local Msg, Name = hl7.parse{vmd = 'example/demo.vmd', data = Data}
   
   -- (1) 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
   
   -- (2) Create update query string      
   local SqlUpdate =
   "   UPDATE patient SET"..
   "\n   LastName = '"..Msg.PID[5][1][1][1].."',"..
   "\n   GivenName = '"..Msg.PID[5][1][2].."',"..
   "\n   Ssn = '"..Msg.PID[19].."'"..
   "\n   WHERE Id = '"..Msg.PID[3][1][1].."'"
   
   -- (3) Update database
   Conn:execute{sql=SqlUpdate, live=true}
end
Description
How to update a database using data from an HL7 message (similar principles apply to other message types)
Usage Details

This example shows how to update a database using data from an HL7 message. The message is parsed into a read-only source HL7 node tree structure, and then used to generate a SQL UPDATE statement.

The process for using other message types follows the same general pattern.

How to use the code:

  • Use a Filter or To Translator script
    • Add the code
    • Load the sample messages from sample_data.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: See the Interface Tutorials (Iguana 5 documentation) section for more information database inserts and updates using other message types.