Schrift
[thread]727[/thread]

Problem mit dem Einlesen (Seite 4)

Leser: 2


<< |< 1 2 3 4 5 6 >| >> 56 Einträge, 6 Seiten
XeroX
 2006-05-24 15:42
#7629 #7629
User since
2006-05-17
27 Artikel
BenutzerIn
[default_avatar]
Code: (dl )
1
2
C:/Programme/xampp/htdocs/modperlasp -- C:/Programme/xampp/htdocs/drow
<br />
renee
 2006-05-24 16:05
#7630 #7630
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Wie Du siehst, wird Dein Skript von einem anderen Ort aus aufgerufen als der Ort an dem sich das Skript befindet. Dein Dictfile befindet sich sicherlich nicht in C:/Programme/xampp/htdocs/modperlasp oder??

Deswegen würde ich den Code von oben etwas abändern:
Code: (dl )
1
2
3
4
5
6
7
8
9
# ganz oben bei anderen use's
use FindBin ();

#... weiterer Code

# in der sub
sub loadDictionary {

my( $DICTFILE ) = $FindBin::RealBin . $_[0];
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
XeroX
 2006-05-26 10:33
#7631 #7631
User since
2006-05-17
27 Artikel
BenutzerIn
[default_avatar]
Ich hab die words.txt in den /modperlapsp/ Folder zusätzlich reinkopiert und deinen Code oben übernommen.Nun bekomme ich wieder folgende Fehlermeldng obwohl ich mir sicher bin das ich alles richitg definiert habe. dbh habe ich auch localhost gesetzt oder ist das falsch ?

Ich hab die words.txt in den modperlaps Folder zusätzlich erinkopiert und deinen Code oben übernommen.

Quote
Can't call method "prepare" on an undefined value at C:/Programme/xampp/perl/site/lib//Mysql.pm line 169.



Das aktuelle Script

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use Mysql;
use FindBin ();

my $DB_TABLE = "drow_dictionary";
my $LANG0 = "Drow";
my $LANG1 = "Common";
my $NOTES = "Notes";
my $dbh = "localhost";

require "drow_pwx.pl"; # Contains database credentials

&loadDictionary( "words.txt" );


sub loadDictionary {

my( $DICTFILE ) = $FindBin::RealBin . $_[0];

#$DICTFILE = $FindBin::Bin . $DICTFILE;
#sub loadDictionary {

my( $DICTFILE ) = $_[0];
my( @lines );

# Read in the dictionary into memory
print "Reading file '$DICTFILE'...";
open( DICTFILE, "$DICTFILE" ) || die( "Couldn't open file: $!" );;
my( @filelines ) = <DICTFILE>;
close( DICTFILE );
foreach $line (@filelines) {
push( @lines, $line ) if( $line =~ m/\w/ );
}
@filelines = ();


print (0+@lines)." lines read.\n";

print "Sorting...";
@lines = sort( @lines );
print "Done.\n";


# Setup Database Connection
$dbh = Mysql->connect( undef, $DATABASE, $DB_USERNAME, $DB_PASSWORD ) ||
die( "Could not establish database connection: ".$Mysql::db_errstr );
$dbh->query( "delete from $DB_TABLE" );

# Cycle through lines, adding them into database
my( $query ) ="insert into $DB_TABLE ($LANG0, $LANG1, $NOTES) values ";
foreach $line (@lines ) {
my( $notes ) = "";
# Simple word0=word1 line
$line =~ s/\`/\'/g; # Change back-quotes to normal quotes
$line =~ s/\_/\ /g; # Change underscores to spaces
$line =~ s/\+/\ /g; # Change plusses to spaces
if( $line =~ m/^([\w\-\'\s\,\+]*)\=(.*)/ ) {
$word1 = $1;
$word2 = $2;

# Get rid of email address of contributed words
if( $word2 =~ m/\=/ ) {
$word2 = $`;
}

# Extract notes if any
if( $word2 =~ m/\((.*)\)/) {
$notes = $1;
$notes =~ s/\'/\\\'/g;
$word2 = $`;
}

# Make SQL safe, replace '+'s with spaces
$word1 =~ s/\'/\\\'/g;
$word2 =~ s/\'/\\\'/g;

# Split if it has commas
my( @words1, @words2 );
if( $word1 =~ m/\,/ ) {
@words1 = split( ',', $word1 );
} else {
@words1 = ($word1);
}
if( $word2 =~ m/\,/ ) {
@words2 = split( ',', $word2 );
} else {
@words2 = ($word2);
}

# Do all combinations if has commas
foreach $word1 (@words1) {
foreach $word2 (@words2) {
$word1 = &trim( $word1 );
$word2 = &trim( $word2 );
if( $notes eq "" ) {
&runQuery( $query."('$word1','$word2', null)" );
} else {
&runQuery( $query."('$word1','$word2', '$notes')" );
}

}
}
} else {
warn( "Can't parse line ==$line==\n" );
}
}

}



sub runQuery {
my( $query ) = $_[0];

#print "$query\n\n";
print '.';

$dbh->query( $query );
if( $dbh->errmsg ne "" ) {
warn( "Database Error: ".$dbh->errmsg."<BR>$_[0]" );
#$dbh->errmsg = "";
}

}


# Trims whitespace off start and end of string
sub trim
{
my( @asz, $sz );
@asz = @_;
foreach $sz (@asz)
{
$sz =~ s/^\s*(.*)$/$1/;
($sz = $`) if ($sz =~ m/\s*$/);
}
return wantarray() ? @asz : $asz[0];
}
renee
 2006-05-26 10:48
#7632 #7632
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Ich würde Dir zur Verwendung von CPAN:DBI mit CPAN:DBD::mysql raten:

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use strict;
use warnings;
use DBI;
use FindBin ();

my $DB_TABLE = "drow_dictionary";
my $LANG0 = "Drow";
my $LANG1 = "Common";
my $NOTES = "Notes";


require "drow_pwx.pl"; # Contains database credentials

loadDictionary( "words.txt" );


sub loadDictionary {
my( $DICTFILE ) = $FindBin::RealBin . $_[0];
my @lines;

# Read in the dictionary into memory
print "Reading file '$DICTFILE'...";
open( DICTFILE,'<', $DICTFILE ) || die( "Couldn't open file: $!" );;
my( @filelines ) = <DICTFILE>;
close( DICTFILE );
foreach $line (@filelines) {
push( @lines, $line ) if( $line =~ m/\w/ );
}
@filelines = ();

print (0+@lines)." lines read.\n";
print "Sorting...";
@lines = sort( @lines );
print "Done.\n";

# Setup Database Connection
my $dbh = DBI->connect("DBI:mysql:$DATABASE:localhost", $DB_USERNAME, $DB_PASSWORD ) ||
die( "Could not establish database connection: ".$DBI::errstr );
$dbh->do( "delete from $DB_TABLE" );

# Cycle through lines, adding them into database
my( $query ) ="insert into $DB_TABLE ($LANG0, $LANG1, $NOTES) values (?,?,?) ";
my $sth = $dbh->prepare($query);

foreach $line (@lines ) {
my( $notes ) = "";
# Simple word0=word1 line
$line =~ s/\`/\'/g; # Change back-quotes to normal quotes
$line =~ s/\_/\ /g; # Change underscores to spaces
$line =~ s/\+/\ /g; # Change plusses to spaces
if( $line =~ m/^([\w\-\'\s\,\+]*)\=(.*)/ ) {
$word1 = $1;
$word2 = $2;

# Get rid of email address of contributed words
if( $word2 =~ m/\=/ ) {
$word2 = $`;
}

# Extract notes if any
if( $word2 =~ m/\((.*)\)/) {
$notes = $1;
$notes =~ s/\'/\\\'/g;
$word2 = $`;
}

# Split if it has commas
my( @words1, @words2 );
if( $word1 =~ m/\,/ ) {
@words1 = split( ',', $word1 );
} else {
@words1 = ($word1);
}
if( $word2 =~ m/\,/ ) {
@words2 = split( ',', $word2 );
} else {
@words2 = ($word2);
}

# Do all combinations if has commas
foreach $word1 (@words1) {
foreach $word2 (@words2) {
$word1 = &trim( $word1 );
$word2 = &trim( $word2 );
if( $notes eq "" ) {
$sth->execute($word1,$word2,undef);
}
else {
$sth->execute($word1,$word2,$notes);
}

}
}
} else {
warn( "Can't parse line ==$line==\n" );
}
}

}


# Trims whitespace off start and end of string
sub trim{
my( @asz, $sz ) = @_;

foreach $sz (@asz){
$sz =~ s/^\s*(.*)$/$1/;
($sz = $`) if ($sz =~ m/\s*$/);
}
return wantarray() ? @asz : $asz[0];
}
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
renee
 2006-05-26 10:52
#7633 #7633
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Vielleicht ist für Dich folgendes ganz lesenswert:
Wiki:Artikel zur CGI-Sicherheit wegen der ?-Notation von CPAN:DBI
PDF zu "Perl mit Datenbanken
Wiki:Artikel über [tt]use strict[/tt]
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
XeroX
 2006-05-26 13:50
#7634 #7634
User since
2006-05-17
27 Artikel
BenutzerIn
[default_avatar]
Hallo,
vielen Danke erstmal das du immer noch ausdauer hast mir zu helfen :)

Also ich hab mal in den Links unten bisschen gelesen und "use strict" conform das my hinzugefügt und germerkt die fehler Seite wird viel kleiner :)

Code: (dl )
1
2
Fehlermeldung: 
Global symbol "$DATABASE" requires explicit package name at C:/Programme/xampp/htdocs/drow/convertDictionary.pl line 37. ,


Allerdings hab ich jetzt eine Frage zu DBI...muss ich nun ins "Config File" $DBI_PASS etc verwenden ?

Und wieso funzt das my vor $DATABASE nicht ? Oo

Dann sollte es ja eigentlich funktionieren oder ?\n\n

<!--EDIT|XeroX|1148637178-->
nepos
 2006-05-26 16:04
#7635 #7635
User since
2005-08-17
1420 Artikel
BenutzerIn
[Homepage] [default_avatar]
Wo kommt das $DATABASE denn ueberhaupt her? Ich seh das nur einmal und das is beim Aufruf der connect-Methode...
XeroX
 2006-05-26 16:06
#7636 #7636
User since
2006-05-17
27 Artikel
BenutzerIn
[default_avatar]
[quote=nepos,26.05.2006, 14:04]Wo kommt das $DATABASE denn ueberhaupt her? Ich seh das nur einmal und das is beim Aufruf der connect-Methode...[/quote]
Code: (dl )
require "drow_pwx.pl";       # Contains database credentials


drow_pwx.pl
Code: (dl )
1
2
3
4
5
6
7
8
use Readonly;

Readonly $DATABASE => "drow";
Readonly $DB_TABLE => "drow_dictionary";
Readonly $DB_USERNAME => "root";
Readonly $DB_PASSWORD => "";

1;
renee
 2006-05-26 20:08
#7637 #7637
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Übernimm die Variablen aus drow_pwx.pl in Dein Skript oder mach:
Code: (dl )
1
2
3
4
5
6
7
8
use strict;

our $DATABASE = "drow";
our $DB_TABLE = "drow_dictionary";
our $DB_USERNAME = "root";
our $DB_PASSWORD = "";

1;
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
Strat
 2006-05-29 20:02
#7638 #7638
User since
2003-08-04
5246 Artikel
ModeratorIn
[Homepage] [default_avatar]
oder besser noch:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
package My::Config;
use strict;
use Readonly;

Readonly our $DATABASE => "drow";
Readonly our $DB_TABLE => "drow_dictionary";
Readonly our $DB_USERNAME => "root";
Readonly our $DB_PASSWORD => "";

1;

und dann im Hauptprogramm
Code: (dl )
1
2
3
4
5
6
require "drow_pwx.pl";       # Contains database credentials
...
my $dbh = DBI->connect("DBI:mysql:$My::Config::DATABASE:localhost", $My::Config::DB_USERNAME, $My::Config::DB_PASSWORD ) ||
die( "Could not establish database connection: ".$DBI::errstr );
$dbh->do( "delete from $My::Config::DB_TABLE" );
# usw.
perl -le "s::*erlco'unaty.'.dk':e,y;*kn:ai;penmic;;print"
http://www.fabiani.net/
<< |< 1 2 3 4 5 6 >| >> 56 Einträge, 6 Seiten



View all threads created 2006-05-17 13:33.