#!/usr/bin/mumps

      s i=$zsqlite("pragma","journal_mode=off")
      s i=$zsqlite("pragma","synchronous=off")

      w "pragma journal_mode=off;",!
      w "pragma synchronous=off;",!

      set date=^date
      set time=^time

      open 1:"meds,old"
      use 1
      set mc=1
      for  do
      . use 1
      . read a
      . i '$t break
      . set ^meds(mc)=a
      . set mc=mc+1
      set mc=mc-1
      close 1

      use 5
#      write "use medical;",!
      write "drop table if exists medadmin;",!
      write "create table medadmin ( ptid char(10), medication_code int, dose int, admin_date date, admin_time time, trnsaction int, icd_code int );",!
      for %=1:1:10 do
      . set ptid=100000
      . for i=1:1:^ptcount(0) do
      .. set ptid=ptid+1000

      .. set kk=0
      .. for k=$o(^picd(ptid,k)) s kk=kk+1
      .. for k=$o(^picd(ptid,k)) if $r(kk)=0 break
      .. if k="" s k=$o(^picd(ptid,""))

      .. set tran=^Tran(0) set ^Tran(0)=^Tran(0)+1
      .. write "insert into medadmin values ( '",ptid,"', "
      .. write $r(mc)+1,", "
      .. write $r(200),", "
      .. write " '",^dates($r(date)+1),"', "
      .. write " '",^time($r(time)+1),"', ",tran,", ",k,"); ",!
