Schrift
[thread]8629[/thread]

'$' in SQL with Perl's Variable?

Leser: 1


<< >> 5 Einträge, 1 Seite
Updecrator
 2007-01-09 09:44
#72979 #72979
User since
2005-11-16
17 Artikel
BenutzerIn
[default_avatar]
Hi, all,

if the perl program reads a SQL file which contains many '$'s,
but perl will interpret them as variables,
how can I handle this problem?
can I disable the interpolation?

Thanks!
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/
renee
 2007-01-09 10:02
#72981 #72981
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Can you show us the code you use?
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/
MisterL
 2007-01-09 10:28
#72982 #72982
User since
2006-07-05
334 Artikel
BenutzerIn
[default_avatar]
Just as an idea: CPAN:SQL::Library
“Perl is the only language that looks the same before and after RSA encryption.”
Updecrator
 2007-01-09 11:42
#72983 #72983
User since
2005-11-16
17 Artikel
BenutzerIn
[default_avatar]
Hi, renee,

thank you very much!  
i think I got the wished answer,
- I use Sqlite3:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#! use /usr/bin/perl;

use strict;
use warnings;
use DBI;

my $result;
my $file = 'testSelect.sql';
my $dbh = DBI->connect("dbi:SQLite:dbname=Test.db","","");

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

$dbh->disconnect;


Best wishes
<< >> 5 Einträge, 1 Seite



View all threads created 2007-01-09 09:44.