Operating System - HP-UX
1748180 Members
3906 Online
108759 Solutions
New Discussion юеВ

how to connect to database in shell script

 
fjatu
Occasional Advisor

how to connect to database in shell script

Whether or not connect to database or excute sql statement in shell script?please give me some documents.thank you!
drivers for hp NIC A5506-60102
6 REPLIES 6
John Carr_2
Honored Contributor

Re: how to connect to database in shell script

Hi

please would you could tell us what type of database and release.

cheers
John.
Tom Geudens
Honored Contributor

Re: how to connect to database in shell script

Hi,
If this is oracle we are talking this would be a way (there's several other ways) :

#!/bin/ksh
sql_logon=$1
sql_script=$2
$ORACLE_HOME/bin/sqlplus << !! 2>&1 >tgd008.log
$sql_logon
@$sql_script;
quit
!!

Hope this helps,
Tom Geudens
A life ? Cool ! Where can I download one of those from ?
Deepak Extross
Honored Contributor

Re: how to connect to database in shell script

If you're talking informix,

#!/usr/bin/ksh
isql <
Printaporn_1
Esteemed Contributor

Re: how to connect to database in shell script

Hi,

for oracle

sqlplus << EOF
select * from tab;
EOF

enjoy any little thing in my life
Magdi KAMAL
Respected Contributor

Re: how to connect to database in shell script

Hi Fiatu,

Here is what you need :

1. Remote database

#sqlplus userName/passWord@OracleInstance< /tmp/output.log 2>&1
select ....
insert ....
delete ....
update ....

EOD


The name "OracleInstance" is the name of the oracle instance defined in the file tnsnames.ora

2.Local database

#sqlplus userName/passWord< /tmp/output.log 2>&1
select ....
insert ....
delete ....
update ....

EOD


HTH

Magdi

harry d brown jr
Honored Contributor

Re: how to connect to database in shell script

It's PERL time:



use strict;
use DBI;

my $dbh = DBI->connect( 'dbi:Oracle:orcl',
'jeffrey',
'jeffspassword',
{
RaiseError => 1,
AutoCommit => 0
}
) || die "Database connection not made: $DBI::errstr";

my $sql = qq{ CREATE TABLE employees ( id INTEGER NOT NULL,
name VARCHAR2(128),
title VARCHAR2(128),
phone CHAR(8)
) };
$dbh->do( $sql );

$dbh->disconnect();


http://www.saturn5.com/~jwb/dbi-examples.html

live free or die
harry
Live Free or Die