Back to the month index |
Back to the list index
|
Andreas Koenig (k@anna.in-berlin.de)
Mon, 2 Sep 1996 21:15:39 +0200
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
- Next message: Joao Marcos Melo Mendes: "Re: [mSQL] insert's query using variables in a perl5..."
- Previous message: D. L. Vander Woude: "[mSQL] Client API MacOS Port?"
Date: Mon, 2 Sep 1996 21:15:39 +0200 Message-Id: <199609021915.VAA16236@anna.in-berlin.de> From: Andreas Koenig <k@anna.in-berlin.de> Subject: Re: [mSQL] mSQL + msql PERL interface>>>>> jcoy@arkansas.net (Coy, John) writes:
coy,> if (! &table_exists("pwent")) {
I always say
unless (grep $_ eq 'mirrorit', $db->listtables) {
coy,> while (@info = getpwent()) {
coy,> #
coy,> # here is where the problem lies. How do I convert the array @info
coy,> # into a string that can be used with $dbh->query()?
coy,> #
coy,> $data = join(",", @info);
coy,> $query = "insert into pwent values ( $data )";
coy,> $sth = $dbh->query($query) || die("Error: Unable to insert data\n");
coy,> }
Good question. Before perl had closures (see man perlref, man perlsub)
this was a real problem. But since then, a tiny hack. I think, you
need MsqlPerl-1.10 for that.
Hope, that helps,
andreas
#!/usr/bin/perl -w
use Msql;
use strict;
# the Insert-Closure-Constructor
sub Msql::icc {
my($db,$table,@fields)=@_;
my($s,@types)=$db->listfields($table) or return;
if (@fields){
# The user wants to input the fields in a different order
my(@tfields,@ttypes,%tfields);
@tfields = $s->name;
@ttypes = $s->type;
@tfields{@tfields} = @ttypes;
@types = @tfields{@ttypes};
} else {
# They are gonna give the arguments in table's order
@fields = $s->name;
@types = $s->type;
}
# return a subroutine reference.
sub {
my(@arr)=@_;
return join " ",
"insert into $table values (",
join(
",",
map {
defined $arr[$_] ?
$types[$_] == &Msql::REAL_TYPE
||
$types[$_] == &Msql::INT_TYPE ?
$arr[$_]+0 :
Msql->quote($arr[$_]) :
"NULL"
}
0..$#types
),
")\n";
}
}
{
my $dbh=Msql->connect("","test");
my $f=$dbh->icc("pwent");
my(@info,$query,$sth);
while (@info = getpwent()) {
# print map {"[$_]"} @info;
# print "\n";
print $query = &$f(@info);
$sth = $dbh->query($query) or die Msql->errmsg;
}
}
--------------------------------------------------------------------------
To remove yourself from the Mini SQL mailing list send a message containing
"unsubscribe" to "unsubscribe" to msql-list-request@bunyip.com. Send a message containing
"info msql-list" to majordomo@bunyip.com for info on monthly archives of
the list. For more help, mail owner-msql-list@bunyip.com NOT the msql-list!