Perl en Español

  1. Home
  2. Tutoriales
  3. Foro
  4. Artículos
  5. Donativos
  6. Publicidad
 

Como conecto un servidor web con uno de bases de datos
Ir a página Anterior  1, 2
 
Publicar nuevo tema   Responder al tema    Foros de discusión -> Bases de Datos
Mensaje Jue Oct 12, 2006 1:30 pm
Juan JOse
Perlero Nuevo
Perlero Nuevo
Registrado: 21 Sep 2006
Mensajes: 10
Ubicación: Desarrollo
Responder citando

Saludos:

Tengo ahora un problema al escribir un Query, quiero ejecutar el siguiente:

SQL:
SELECT count(*) FROM empleados WHERE rfc=algo AND nombre LIKE 'paterno%materno'


Pero necesito que paterno y materno sean variables. Intenté el siguiente código:

Perl:
47: $prep="select count(*) from empleados where  rfc=? and nombre like    \'?\%?\' ";
48: my $sth=$dbh prepare($prep);
49: $sth->execute($rfc,$paterno,$materno);
50: $n_registros = $sth->fetchrow_array();
51: print "$n_registros registros encontrados";
52: $sth->finish
95: if($n_registros eq "0" ){
105: if($n_registros eq "1" )

Y me salen los siguientes mensajes de error:

Código:
[Thu Oct 12 15:15:05 2006] [error] [client xxx.xxx.xxx.xxx] DBD::Informix::st execute failed: called with 3 bind variables when 1 are needed at /var/www/cgi-bin/sueldo.pl line 49., referer: http://xxx.xxx.xxx.xxx/cgi-bin/sueldo.pl
[Thu Oct 12 15:15:05 2006] [error] [client xxx.xxx.xxx.xxx] DBD::Informix::st fetchrow_array failed: SQL: -400: Fetch attempted on unopen cursor. at /var/www/cgi-bin/sueldo.pl line 50., referer: http://xxx.xxx.xxx.xxx/cgi-bin/sueldo.pl
[Thu Oct 12 15:15:05 2006] [error] [client xxx.xxx.xxx.xxx] Use of uninitialized value in concatenation (.) or string at /var/www/cgi-bin/sueldo.pl line 51., referer: http://xxx.xxx.xxx.xxx/cgi-bin/sueldo.pl
[Thu Oct 12 15:15:05 2006] [error] [client xxx.xxx.xxx.xxx] Use of uninitialized value in string eq at /var/www/cgi-bin/sueldo.pl line 95., referer: http://xxx.xxx.xxx.xxx/cgi-bin/sueldo.pl
[Thu Oct 12 15:15:05 2006] [error] [client xxx.xxx.xxx.xxx] Use of uninitialized value in string eq at /var/www/cgi-bin/sueldo_base1.pl line 105., referer: http://xxx.xxx.xxx.xxx/cgi-bin/sueldo.pl


Ojalá alguno de uds pueda ayudarme, aún me falta mucho por aprender de Perl. Gracias nuevamente.
Mensaje Jue Oct 12, 2006 6:29 pm
explorer
Moderador
Moderador
Registrado: 24 Jul 2005
Mensajes: 4212
Ubicación: Valladolid, España
Responder citando

Podrías probar a crear la sentencia SQL antes de 'prepararla':

Perl:
# ...
$prep = "select count(*) from empleados where  rfc=? and nombre like \'$paterno%$materno\'";
my $sth = $dbh->prepare( $prep );
$sth->execute( $rfc );
$n_registros = $sth->fetchrow_array();
# ...


No deja de ser una chapucilla, porque además no contaríamos con el trabajo de escape de caracteres que hace execute, pero para el caso de que $paterno y $materno sean strings 'normales', se podría intentar algo así.

Quizás esto otro funcione (sacado del manual del DBI):

Perl:
$prep = "select count(*) from empleados where  rfc=? and nombre like ?";
my $sth = $dbh->prepare( $prep );
$sth->execute( $rfc, "$paterno%$materno" );
Mensaje Vie Oct 13, 2006 1:32 pm
Juan JOse
Perlero Nuevo
Perlero Nuevo
Registrado: 21 Sep 2006
Mensajes: 10
Ubicación: Desarrollo
Responder citando

Saludos:

Se resolvio el problema escibiendo la sentencia como me recomendaste

$prep = "select count(*) from cheque where rfc=? and nombre like \"$paterno\%$materno\%\" ";
my $sth = $dbh->prepare($prep);
$sth->execute($rfc);

Gracias.
Publicar nuevo tema   Responder al tema    Foros de discusión -> Bases de Datos Todas las horas son GMT - 6 Horas
Ir a página Anterior  1, 2
Página 2 de 2



Powered by phpBB © 2001, 2005 phpBB Group