Schrift
Wiki:Tipp zum Debugging: use Data::Dumper; local $Data::Dumper::Useqq = 1; print Dumper \@var;
[thread]3647[/thread]

DBI MySQL-Abfrage mit Platzhalter: SQL-Statement ausgeben

Leser: 1


<< |< 1 2 >| >> 19 Einträge, 2 Seiten
MartinR
 2005-12-19 12:11
#33858 #33858
User since
2004-06-17
305 Artikel
BenutzerIn
[default_avatar]
Hallo,

ich erstelle meine SQL-Abfragen ggf. mit Platzhaltern.

Code: (dl )
1
2
3
4
$sth = $dbh->prepare(qq{
INSERT INTO tabelle (spalte_1, spalte_2, spalte_3 )
VALUES (?, ?, ?)
});


um dann später die Abfrage auszuführen mit

Code: (dl )
$sth->execute( $wert_1, $wert_2, $wert_3 );


Bekomme ich nun irgendwie raus welcher SQL-String (ohne ihn selbst zusammenzusetzen) wirklich von der DB abgearbeitet wurde? Z.B. zum Debuggen. In der DBI-Doku habe ich nichts gefunden.

cu und Danke schon mal
renee
 2005-12-19 12:20
#33859 #33859
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Probier mal, ob Du was ueber
Code: (dl )
print $sth->{Statement};
rausbekommst...
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/
MartinR
 2005-12-19 12:32
#33860 #33860
User since
2004-06-17
305 Artikel
BenutzerIn
[default_avatar]
[quote=renee,19.12.2005, 11:20]Probier mal, ob Du was ueber
Code: (dl )
print $sth->{Statement};
rausbekommst...[/quote]
Danke, aber das gibt mir leider auch nur die Platzhaltersyntax ohne die tatsächlichen Werte zurück.
renee
 2005-12-19 12:41
#33861 #33861
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Das waere doch mal einen Feature Request wert... Ich frage bei Tim mal an...
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/
pq
 2005-12-19 15:55
#33862 #33862
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
schau mal in der doku unter trace. damit kriegst du auf jeden fall das
statement (aber auch noch ne menge anderen output)
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem
renee
 2005-12-20 01:26
#33863 #33863
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Antwort von Tim:
Quote
On Mon, Dec 19, 2005 at 11:47:59PM +0100, module@renee-baecker.de wrote:
> Hi Tim,
>
> is it possible to introduce one new "field" (attribute) for an executed
> statement?
>
> With $sth->{Statement} you just get the "prepared" statement, but if you
> use placeholders and you want to see the executed statement, this is not
> very helpful.

For many databases the placeholders are effectively merged into the
statement on the server, in which case the client may not be able to
produce an SQL statement that matches exactly what the server will do.
(Consider attributes to bind_param() that alter behaviour, for example.)

The ParamValues attribute may be of use to you, if your driver supports
it. See archives for previous discussions on this topic:
http://www.google.com/search?q=ParamValues+bunce

If your driver emulates placeholder itself then it could add a
driver-private attribute that provides the expended statement.

Tim.
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/
Arkhen2
 2005-12-20 11:15
#33864 #33864
User since
2005-03-11
25 Artikel
BenutzerIn
[default_avatar]
Also würde sowas in der Art ( ohne Datentyp berücksichtigt ):

Code: (dl )
1
2
3
4
5
6
my $sql = $sth->{Statement};

for( keys %{$sth->{ParamValues}} )
{
   $sql =~ s/\?/$sth->{ParamValues}->{$_}/;
}


funktionieren ?
renee
 2005-12-21 00:21
#33865 #33865
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Müsste wohl eher so aussehen:
Code: (dl )
1
2
3
4
5
my $sql = $sth->{Statement};

for(sort{$a <=> $b}keys %{$sth->{ParamValues}} ){
$sql =~ s/\?/$sth->{ParamValues}->{$_}/;
}
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/
MartinR
 2005-12-21 08:23
#33866 #33866
User since
2004-06-17
305 Artikel
BenutzerIn
[default_avatar]
Hi, und Danke schon mal an alle.

Leider bringt mir der code von renee folgende Fehlermeldung:

Can't get DBI::st=HASH(0x84e9644)->{ParamValues}: unrecognised attribute at ...

Das selbe übrigens mit TraceLevel.

Liegt wohl auch daran, dass ich momentan nur auf einer alten Maschine mit Perl 5.00 und entsprechend altem DBI und MySQL zugreifen kann. Aber in vier Wochen bekomme ich eine aktuelle LAMP-Version. Probiere eben dann weiter ...
renee
 2005-12-21 10:41
#33867 #33867
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Welche DBI-Version hast Du denn?
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/
<< |< 1 2 >| >> 19 Einträge, 2 Seiten



View all threads created 2005-12-19 12:11.