C Collection of code snippets by Arne Vajhøj 
C (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
      program sqlr
      implicit none
      external std_ph,std_bn,pgsql_ph,pgsql_bn,ora_ph,ora_bn
      character*32 std_ph,std_bn,pgsql_ph,pgsql_bn,ora_ph,ora_bn
      call test('arnepc4',9001,'arne','topsecret',std_ph,std_bn)
      call test('arnepc4',9002,'arne','topsecret',pgsql_ph,pgsql_bn)
      call test('arnepc4',9003,'arne','topsecret',std_ph,std_bn)
      call test('arnepc4',9004,'arne','topsecret',ora_ph,ora_bn)
      call test('arnepc4',9005,'arne','topsecret',std_ph,std_bn)
      call test('arnepc4',9006,'arne','topsecret',std_ph,std_bn)
      call test('arnepc4',9007,'arne','topsecret',std_ph,std_bn)
      call test('arnepc4',9008,'arne','topsecret',std_ph,std_bn)
      call test('arnepc4',9010,'arne','topsecret',std_ph,std_bn)
      call test('arnepc4',9011,'arne','topsecret',std_ph,std_bn)
      call test('arnepc4',9013,'arne','topsecret',std_ph,std_bn)
      end
c******** test one database
      subroutine test(host,port,un,pw,ph,bn)
      implicit none
      character*(*) host,un,pw
      external ph,bn
      character*32 ph,bn
      integer*4 port
      integer*8 con
      character*1 socket
      integer*4 f1
      integer*4 t1_get_one
      write(*,*) 'Connect host=',host,' port=',port
c  open
      socket = '?'
      call sqlr$con_alloc(host,port,socket,un,pw,0,1,con)
c  test t1_get_one
      f1=t1_get_one(con,'BB',ph,bn)
      write(*,*) f1
c  test t1_get_all
      call t1_dump(con)
c  test t1_put
      call t1_put(con,999,'XXX',ph,bn)
      call t1_dump(con)
c  test t1_remove
      call t1_remove(con,999,ph,bn)
      call t1_dump(con)
c  close
      call sqlr$con_free(con)
      return
      end
c******** print all records
      subroutine t1_dump(con)
      implicit none
      integer*8 con
      structure /t1/
        integer*4 f1
        character*50 f2
      end structure
      integer*4 MAX_REC
      parameter (MAX_REC=100)
      record /t1/buf(MAX_REC)
      integer*4 n,i
      integer*4 t1_get_all
      n=t1_get_all(con,buf,MAX_REC)
      do 100 i=1,n
        write(*,*) buf(i).f1,' ',buf(i).f2
100   continue
      return
      end
c******** get f1 given f2
      integer*4 function t1_get_one(con,f2,ph,bn)
      implicit none
      integer*8 con
      character*(*) f2
      external ph,bn
      character*32 ph,bn
      include 'sqlr$fortran:sqlrdef.for'
      integer*4 stat,res,slen,f1
      integer*8 curs,temp
      character*50 s
c  get cursor
      call sqlr$cur_alloc(con,curs)
c  prepare
      call sqlr$cur_preparequery(curs,'SELECT f1 FROM t1 WHERE f2 = '//
     +                                ph('1','f2','text'))
c  bind parameters
      call sqlr$cur_inputbindstring(curs,bn('1','f2'),f2,len(f2))
c  execute
      stat=sqlr$cur_executequery(curs,res)
      if (mod(stat,2).eq.0) callcurs_exit(curs)
c  process result
      stat=sqlr$cur_getfieldbyindex(curs,0,0,s,slen)
      if(mod(stat,2).eq.0) call other_exit('Row not found')
      call sqlr$cur_getfieldasintbyind(curs,0,0,temp)
      f1=temp
      call sqlr$con_endsession(con)
c  close cursor
      call sqlr$cur_free(curs)
      t1_get_one=f1
      return
      end
c******** get all records
      integer*4 function t1_get_all(con,buf,bufsiz)
      implicit none
      integer*8 con
      structure /t1/
        integer*4 f1
        character*50 f2
      end structure
      record /t1/buf(*)
      integer*4 bufsiz
      include 'sqlr$fortran:sqlrdef.for'
      integer*8 curs,row,temp
      integer*4 stat,count,slen
      character*50 s
c  get cursor
      call sqlr$cur_alloc(con,curs)
c  execute
      stat=sqlr$cur_sendquery(curs,'select f1,f2 from t1')
      if(mod(stat,2).eq.0) call curs_exit(curs)
c  process result
      count=0
      call sqlr$cur_setresultsetbuffersize(curs,100)
      row=0
      count=0
100   if(count.ge.bufsiz) goto 200
      stat=sqlr$cur_getfieldbyindex(curs,row,0,s,slen)
      if(mod(stat,2).eq.0) goto 200
      count=count+1
      call sqlr$cur_getfieldasintbyind(curs,row,0,temp)
      buf(count).f1=temp
      call sqlr$cur_getfieldbyindex(curs,row,1,s,slen)
      buf(count).f2=s(1:slen)
      row=row+1
      goto 100
200   call sqlr$con_endsession(con)
c  close cursor
      call sqlr$cur_free(curs)
      t1_get_all=count
      return
      end
c******** add record
      subroutine t1_put(con,f1,f2,ph,bn)
      implicit none
      integer*8 con
      integer*4 f1
      character*(*) f2
      external ph,bn
      character*32 ph,bn
      include 'sqlr$fortran:sqlrdef.for'
      integer*4 stat,res
      integer*8 curs,temp,n
c  get cursor
      call sqlr$cur_alloc(con,curs)
c  prepare
      call sqlr$cur_preparequery(curs,'INSERT INTO t1 VALUES('//
     +                                ph('1','f1','int')//','//
     +                                ph('2','f2','text')//')')
c  bind parameters
      temp=f1
      call sqlr$cur_inputbindlong(curs,bn('1','f1'),temp)
      call sqlr$cur_inputbindstring(curs,bn('2','f2'),f2,len(f2))
c  execute
      stat=sqlr$cur_executequery(curs,res)
      if (mod(stat,2).eq.0) callcurs_exit(curs)
      call sqlr$cur_affectedrows(curs,n)
      if(n.ne.1) call other_exit('INSERT did not insert 1 row')
      call sqlr$con_endsession(con)
c  close cursor
      call sqlr$cur_free(curs)
      return
      end
c******** delete record
      subroutine t1_remove(con,f1,ph,bn)
      implicit none
      integer*8 con
      integer*4 f1
      external ph,bn
      character*32 ph,bn
      include 'sqlr$fortran:sqlrdef.for'
      integer*4 stat,res
      integer*8 curs,temp,n
c  get cursor
      call sqlr$cur_alloc(con,curs)
c  prepare
      call sqlr$cur_preparequery(curs,'DELETE FROM t1 WHERE f1 = '//
     +                                ph('1','f1','int'))
c  bind parameters
      temp=f1
      call sqlr$cur_inputbindlong(curs,bn('1','f1'),temp)
c  execute
      stat=sqlr$cur_executequery(curs,res)
      if (mod(stat,2).eq.0) callcurs_exit(curs)
      call sqlr$cur_affectedrows(curs,n)
      if(n.ne.1) call other_exit('DELETE did not delete 1 row')
      call sqlr$con_endsession(con)
c  close cursor
      call sqlr$cur_free(curs)
      return
      end
c******** error handling functions
      subroutine con_exit(con)
      implicit none
      integer*8 con
      integer*4 msglen
      character*1024 msg
      call sqlr$con_errormessage(con,msg,msglen)
      write(*,*) 'SQLR connection error: ',msg(1:msglen)
      stop
      end
c********
      subroutine curs_exit(curs)
      implicit none
      integer*8 curs
      integer*4 msglen
      character*1024 msg
      call sqlr$cur_errormessage(curs,msg,msglen)
      write(*,*) 'SQLR cursor error: ',msg(1:msglen)
      stop
      end
c********
      subroutine other_exit(msg)
      implicit none
      character*(*) msg
      write(*,*) msg
      stop
      end
c******** standard (ODBC, DB2, MySQL)
      character*32 function std_ph(num,nam,typ)
      character*(*) num,nam,typ
      std_ph='?'
      return
      end
c********
      character*32 function std_bn(num,nam)
      character*(*) num,nam
      std_bn=num
      return
      end
c******** PostgreSQL
      character*32 function pgsql_ph(num,nam,typ)
      character*(*) num,nam,typ
      pgsql_ph='$'//num//'::'//typ
      return
      end
c********
      character*32 function pgsql_bn(num,nam)
      character*(*) num,nam
      pgsql_bn=num
      return
      end
c******** Oracle
      character*32 function ora_ph(num,nam,typ)
      character*(*) num,nam,typ
      ora_ph=':'//nam
      return
      end
c********
      character*32 function ora_bn(num,nam)
      character*(*) num,nam
      ora_bn=nam
      return
      end
