Oracle
Posts 11-11 of 11
- Back
- Next
-
Thomas GrünthalThe company name is only visible to registered members.Re^2: Inhalt einer Textdatei in Oracle als varchar2 speichern
Hallo Herr Wagner
Da ich ein großer Fan von PERL bin, habe ich ein Perl-Skript geschrieben. Perl ist auch für Windows verfügbar (siehe Kommentar unten). Zusätzlich muss das DBI-Modul aus dem Internet heruntergeladen werden - dann sollte es laufen.
Den folgenden Source-Code in eine Datei (z.B. insert_login.pl) schreiben und mit der Kommandozeile
perl insert_login.pl user/pwd@db_name dateiname
aufrufen.
#!/usr/bin/perl -w
#---------------------------------------------------------------------
# This is perl, v5.8.7 built for MSWin32-x86-multi-thread
# (with 7 registered patches, see perl -V for more detail)
#
# Copyright 1987-2005, Larry Wall
#
# Binary build 813 [148120] provided by ActiveState
http://www.ActiveState.com
# ActiveState is a division of Sophos.
# Built Jun 6 2005 13:36:37
#
# Perl may be copied only under the terms of either the Artistic License or the
# GNU General Public License, which may be found in the Perl 5 source kit.
#
# Complete documentation for Perl, including FAQ lists, should be found on
# this system using `man perl' or `perldoc perl'. If you have access to the
# Internet, point your browser at
http://www.perl.org/, the Perl Home Page.
#---------------------------------------------------------------------
use DBI;
use strict;
my @drivers = DBI->available_drivers;
die "Keine Treiber gefunden!\n" unless @drivers;
my $connect_string = shift||die ("Connect-String user\/pwd\@db fehlt\n"); # = $ARGV[0]
(my $user,my $pwd,my $dbname) = $connect_string =~ /^([^\/]*)\/?([^\@]*)\@?(.*)/;
die ("User-Name fehlt\n") unless ($user);
die ("Passwort-Name fehlt\n") unless ($pwd);
die ("DB-Name fehlt\n") unless ($dbname);
my $dbh = DBI->connect( "dbi:Oracle:$dbname", "$user", "$pwd" )
or die "Fehler beim Verbindungsausbau: $DBI::errstr\n";
$dbh->{RaiseError} = 1;
$dbh->{AutoCommit} = 0;
my $sth;
my $in_filename = shift||die ("Filename nicht angegeben!\n"); # = ARGV[1]
my $zeile;
my $inhalt;
#--------------------------------------------------------------------------
# LOG-Datei einlesen
#--------------------------------------------------------------------------
open (IFILE,"$in_filename") or die ("Fehler beim Oeffnen von $in_filename, RC=$!");
while ($zeile = <IFILE>) {
$inhalt .= $zeile;
}
close IFILE;
#--------------------------------------------------------------------------
# ip_address auslesen, wenn IPaddress nicht definiert ist
#--------------------------------------------------------------------------
my $ip_address = $ENV{"IPaddress"};
unless ($ip_address) {
system("ping -n 1 $ENV{\"computername\"} > ip.lst");
open (IFILE,"ip.lst");
while ($zeile = <IFILE>) {
($ip_address) = $zeile =~ /^Ping.*\[(.*)\]/;
last if ($ip_address);
}
close IFILE;
unlink ("ip.lst");
}
#--------------------------------------------------------------------------
# ab in die Datenbank
#--------------------------------------------------------------------------
$sth = $dbh->prepare(q{
BEGIN
INSERT INTO logins (Datum,username,rechnername,IPaddress,Logonserver,LOCAL_ENV,CUSTOM_TEXT)
VALUES (SYSDATE,:username,:computername,:ipaddress,:loc,:local_env,:custom_text);
END;
});
$sth->bind_param(":username",$ENV{"Username"});
$sth->bind_param(":computername",$ENV{"computername"});
$sth->bind_param(":ipaddress",$ip_address);
$sth->bind_param(":loc",$ENV{"LOC"}||$ENV{"LOGONSERVER"});
$sth->bind_param(":local_env",$ENV{"LOCAL_ENV"}||"Test");
$sth->bind_param(":custom_text",$inhalt);
$sth->execute||die ("insert into logins fehlgeschlagen\n");
$sth->finish if $sth;
$dbh->commit;
This post was modified on 14 Jan 2012 at 12:38 am.- 13 Jan 2012, 6:22 pm
- Back
- Next
