在我们医院的医保实施过程中,需要实现这样的功能:医保结算电脑需要在命令行(不是浏览器)通过CGI和服务器通信,上传sql语句,把查询到的结果通过cgi传回来。

   我们首先实现一个最简单的情况,就是只查一条记录,就是select xm from mzpatient where patientid='0000002'这种形式。
   在服务器端编写一个cgi程序yb_cgi_reader.cgi,内容是:

 

点击(此处)折叠或打开

  1. #!"D:\Strawberry\perl\bin\perl

  2. use CGI;

  3. use DBI;
  4. $dbh = DBI->connect("DBI:SQLite:dbname=his.db");

  5. $co = new CGI;

  6. print $co->header,

  7. $co->start_html(
  8.   -title=>'CGI Example',
  9.   -author=>'yourName',
  10.   -BGCOLOR=>'white',
  11.   -LINK=>'red'
  12. );

  13. if ($co->param()) {

  14.  $sql = $co->param('sql');

  15.  $sql5 = qq($sql) ;
  16.  $sth5 = $dbh->prepare( $sql5 ) or print "Couldn't prepare statement: " . $dbh5->errstr;
  17.  $sth5->execute() or print "Couldn't execute statement:
    在客户端编写程序sub_yb_cgisender.pl,内容是:

点击(此处)折叠或打开

  1. use HTTP::Request::Common;
  2. use LWP::UserAgent;

  3. sub sendsql()
  4. {
  5.  $sql = shift;

  6.  $user_agent = LWP::UserAgent->new;
  7.  $request = POST 'http://192.168.0.6/perl/his2/yb_cgi_reader.cgi',
  8.  [sql => "$sql", text => ''];
  9.  $response = $user_agent->request($request);
  10.  return $response->as_string;
  11. }

  12. 1
  这样在Perl程序中这样调用这个子函数:

点击(此处)折叠或打开

  1. require "sub_yb_cgisender.pl";

  2.  $sql = qq(select rybm from zd_people where peoplename = '$operer');
  3.  $result = &sendsql($sql);
  4.  if ($result=~/<em>(.*?)<\/em>/)
  5.  { $sfryxm = $1; }
  经过测试,sql语句的结果可以正常读取。
 再写个复杂一些的,一个sql语句包含多项字段。服务端yb_cgi_reader2.cgi内容为

点击(此处)折叠或打开

  1. #!"D:\Strawberry\perl\bin\perl
  2. # 处理由其他电脑命令行传来的sql语句

  3. use CGI;

  4. use DBI;
  5. $dbh = DBI->connect("DBI:SQLite:dbname=his.db");

  6. $co = new CGI;

  7. print $co->header,

  8. $co->start_html(
  9.   -title=>'CGI Example',
  10.   -author=>'yourName',
  11.   -BGCOLOR=>'white',
  12.   -LINK=>'red'
  13. );

  14. if ($co->param()) {

  15.  $sql = $co->param('sql');

  16.  $sql5 = qq($sql) ;
  17.  $sth5 = $dbh->prepare( $sql5 ) or print "Couldn't prepare statement: " . $dbh5->errstr;
  18.  $sth5->execute() or print "Couldn't execute statement:
客户端sub_yb_cgisender2.pl内容为:

点击(此处)折叠或打开

  1. use HTTP::Request::Common;
  2. use LWP::UserAgent;

  3. sub sendsql2()
  4. {
  5.  $sql = shift;

  6.  $user_agent = LWP::UserAgent->new;
  7.  $request = POST 'http://192.168.0.6/perl/his2/yb_cgi_reader2.cgi',
  8.  [sql => "$sql", text => ''];
  9.  $response = $user_agent->request($request);
  10.  return $response->as_string;
  11. }

  12. 1

调用实例:


点击(此处)折叠或打开

  1. require "sub_yb_cgisender2.pl";


  2.  # 识别刚录入的医保类型的处方编号
  3.  $sql = qq(select reg_note.regnoteid,mzpatient.patientid,mzpatient.sexid,mzpatient.patientname,reg_note.notetype from mzpatient,reg_note where mzpatient.patientid=reg_note.patientid and mzpatient.leixing='yibao' and reg_note.tobilled='0' and reg_note.operer !='D' and reg_note.jshid is null order by reg_note.makenotetime desc limit 0,8);
  4.  $result = &sendsql2($sql);
  5.  @line = ($result=~/<em>(.*?)<\/em>/isg);
  6.  foreach $l (@line)
  7.  {
  8.   @temp = split (" ",$l);
  9.   $regnoteid[$i] = $temp[0];
  10.   $patientid[$i] = $temp[1];
  11.   $sexid[$i] = $temp[2];
  12.   $patientname[$i] = $temp[3];
  13.   $notetype[$i++] = $temp[4];
  14.  }
一次查询多项字段,运行正常。
09-03 18:12