Thread '$' in SQL with Perl's Variable? (4 answers)
Opened by Updecrator at 2007-01-09 09:44

renee
 2007-01-09 10:01
#72980 #72980
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
If you read in a file, a $ is not interpolated (if you do not use eval).

This works fine:

table:
Code: (dl )
1
2
3
4
5
6
7
mysql> desc tbl_Test;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| test | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
1 row in set (0.00 sec)


test.sql:[sql]INSERT INTO tbl_Test VALUES('$test');[/sql]

skript:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/perl

use strict;
use warnings;
use DBI;

my $file = 'test.sql';

my ($user,$pass,$db,$host) = ("xx","xx","xx","localhost");
my $dbh = DBI->connect("DBI:mysql:$db:$host",$user,$pass) or die $DBI::errstr;

open my $fh,'<',$file or die $!;
while(my $stmt = <$fh>){
$dbh->do($stmt) or die $dbh->errstr();
}
close $fh;


result:
Code: (dl )
1
2
3
4
5
6
7
mysql> select * from tbl_Test;
+-------+
| test |
+-------+
| $test |
+-------+
1 row in set (0.00 sec)
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/

View full thread '$' in SQL with Perl's Variable?