免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4293 | 回复: 2
打印 上一主题 下一主题

如何在shell中运行sql语句? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-08-01 14:20 |只看该作者 |倒序浏览
我的数据库已经建好,在sqlplus里可以访问到局域网中一台windows的数据库。

我现在想实现在的是把一段sql语句存为一个shell文件。在执行shell文件的时候就可以打开sqlplus,并运行里面的sql语句。

如: vi sqltest.sh

$ sqlplus test/test@testDB
$ select * from tab;

但运行结果只是连到数据库并出现sql>;提示符,第二行的sql语句并不能执行。请教教我该怎么写呢?

论坛徽章:
0
2 [报告]
发表于 2005-08-01 14:42 |只看该作者

如何在shell中运行sql语句?

经过shell坛子里lovelyarry的帮助已经解决。现把成功代码发出来!希望有同样问题的朋友能节约一丁点时间,也算是对lovelyarry的报答吧。


sqlplus test/test@testDB<<!
select * from tab;
exit
!

求职 : 数据库管理员
论坛徽章:
0
3 [报告]
发表于 2005-08-02 09:31 |只看该作者

如何在shell中运行sql语句?

還有在
http://www.zdnet.com.cn/developer/code/story/0,3800066897,39225241,00.htm 上學到的方法
Example:
alias sp=sp.sh
sp "connect test/test"
sp "select table_name from user_tables"
sp "select  sysdate from dual"
sp "exit"


spd.pl

  1. #!/usr/bin/perl -w
  2. # spd.pl - the SQL*Plus daemon server
  3. use strict;
  4. use Expect;
  5. use Socket;

  6. # set up expect
  7. # -- timeout after about 10 minutes
  8. my $timeout = 600;
  9. # -- scan for the SQL*Plus prompt
  10. my $prompt = 'SQL>;';
  11. my $exp = new Expect();
  12. $exp->;raw_pty(1);
  13. $exp->;log_stdout(0);
  14. $exp->;spawn('sqlplus','/nolog') || die "unable to spawn sqlplus: $!";
  15. $exp->;expect($timeout,'-ex',$prompt) || die $exp->;error();
  16. print $exp "set sqlprompt $prompt;\n";
  17. $exp->;expect($timeout,'-ex',$prompt) || die $exp->;error();
  18. $exp->;clear_accum();

  19. my $name = "/tmp/sp_$ENV{USER}";
  20. unlink($name);
  21. socket(S,PF_UNIX,SOCK_STREAM,0) || die "socket: $!";
  22. bind(S,sockaddr_un($name)) || die "bind: $!";
  23. listen(S,SOMAXCONN) || die "listen: $!";

  24. while(accept(C,S))
  25. {
  26.     # single threaded to avoid confusion
  27.     my $cmd = <C>;;
  28.     $cmd =~ s/[\r\n]*$//g;
  29.     print $exp $cmd,"\n";
  30.     if ($cmd =~ /^exit;$/mi)
  31.     {
  32.         print C "exit.\n";
  33.         close C;
  34.         last;
  35.     }
  36.     print C $cmd."\n" ;
  37.     $exp->;expect($timeout,$prompt) || die $exp->;error() ;
  38.     print C $exp->;before();
  39.     close C;
  40. }

  41. $exp->;soft_close();
  42. close S;
  43. unlink($name);
  44. exit;

复制代码


spc.pl

  1. #!/usr/bin/perl -w
  2. # spc.pl
  3. use Socket;
  4. use strict;
  5. my $cmd = join(' ',@ARGV);
  6. my $name = "/tmp/sp_$ENV{USER}";
  7. my $proto = getprotobyname('udp');

  8. $cmd=$cmd.";" if ( not $cmd =~ /.+;$/)  ;


  9. socket(S,PF_UNIX,SOCK_STREAM,0) or die "socket: $!";
  10. select S; $| = 1; select STDOUT;
  11. connect(S,sockaddr_un($name)) or die "connect: $!";

  12. print S $cmd,"\n";
  13. print while (<S>;);
  14. close(S);
  15. exit;

复制代码


sp.sh

  1. #!/bin/sh

  2. MF=$0;

  3. if [ -h $0 ] ; then
  4.         MFILE=$(ls -l $0 );
  5.         MF=${MFILE#*->;};
  6. fi

  7. MWD=$( dirname $MF );

  8. f=/tmp/sp_$USER

  9. if [ ! -e $f ]; then
  10.     perl $MWD/spd.pl &
  11.     sleep 1 ;
  12. fi
  13. perl $MWD/spc.pl "$*"

复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP