Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re^4: DBI, and date type SQLite

by nando (Acolyte)
on Oct 09, 2011 at 22:00 UTC ( #930506=note: print w/replies, xml ) Need Help??

in reply to Re^3: DBI, and date type SQLite
in thread DBI, and date type SQLite

Thanks!... The code is:

use CGI; use DBI; use Encode; my $q = CGI->new; my $fecha= $q->param('theDate'); my $orden= $q->param('orden'); print $q->header, $q->start_html(-lang=>'es-SP',-title=> 'Reservas CPA +'), $q->h2({-align=>'CENTER'},decode("utf-8","Reservas para el día: " +)."$fecha ordenadas por $orden\n\n"); my $dbh = DBI->connect("dbi:SQLite:dbname=cpa-simple.db", "", "", {Rai +seError =>1 } ); my $sth = $dbh->prepare( q ( select id_reserva,activo,tipo_reserva,ent +idad_nombre_reserva,nombre,apellidos,telefono,inicio_reserva,fin_rese +rva,periodicidad,horai,horaf,nombre_admin from reserva,activos,nombre +,administ where\ tivos_reserva_id=activos.id_activos and reserva.nombre_reserva_id=nomb +re.id_dni and reserva.administ_reserva_id=administ.id_administ and ju +lianday( ? ) between julianday ( inicio_reserva ) and julianday ( fin +_reserva ) order by ?)) or\ die "No puedo preparar la sentencia: " .$dbh->errstr; my @data; $sth->execute("$fecha","$orden") or die "Algo está pasando... ¡Llama a + los bomberos!: " . $sth->errstr; my @datautf8; print $q->start_table({-border=>'1', -align=>'center',-cellpadding=>'6'} +),"\n\n", $q->start_Tr, $q->start_td({align=>'center'}),"\n", $q->p(decode("utf-8","Núm.")),"\n", $q->end_td, "\n", $q->start_td({align=>'center'}),"\n", $q->p(decode("utf-8","Material")),"\n", $q->end_td, "\n", $q->start_td({align=>'center'}),"\n", $q->p(decode("utf-8","Carácter")),"\n", $q->end_td, "\n", $q->start_td({align=>'center'}),"\n", $q->p(decode("utf-8","Entidad")),"\n", $q->end_td, "\n", $q->start_td({align=>'center'}),"\n", $q->p(decode("utf-8","Nombre")),"\n", $q->end_td, "\n", $q->start_td({align=>'center'}),"\n", $q->p(decode("utf-8","Apellidos")),"\n", $q->end_td, "\n", $q->start_td({align=>'center'}),"\n", $q->p(decode("utf-8","Teléfono")),"\n", $q->end_td, "\n", $q->start_td({align=>'center'}),"\n", $q->p(decode("utf-8","Inicio de reserva")),"\n", $q->end_td, "\n", $q->start_td({align=>'center'}),"\n", $q->p(decode("utf-8","Fin de reserva")),"\n", $q->end_td, "\n", $q->start_td({align=>'center'}),"\n", $q->p(decode("utf-8","Periódica")),"\n", $q->end_td, "\n", $q->start_td({align=>'center'}),"\n", $q->p(decode("utf-8","Hora inicio")),"\n", $q->end_td, "\n", $q->start_td({align=>'center'}),"\n", $q->p(decode("utf-8","Hora finalización")),"\n", $q->end_td, "\n", $q->start_td({align=>'center'}),"\n", $q->p(decode("utf-8","Administrador")),"\n", $q->end_td, "\n", $q->end_Tr,"\n"; while ( @data = $sth->fetchrow_array()){ @datautf8 = map(decode("utf-8",$_), @data); print $q->start_Tr, $q->start_td({-align=>'left'}),"\n", $q->p("$datautf8[0]"),"\n", $q->end_td,"\n", $q->start_td({-align=>'left'}),"\n", $q->p("$datautf8[1]"),"\n", $q->end_td,"\n", $q->start_td({-align=>'left'}),"\n", $q->p("$datautf8[2]"),"\n", $q->end_td,"\n", $q->start_td({-align=>'left'}),"\n", $q->p("$datautf8[3]"),"\n", $q->end_td,"\n", $q->start_td({-align=>'left'}),"\n", $q->p("$datautf8[4]"),"\n", $q->end_td,"\n", $q->start_td({-align=>'left'}),"\n", $q->p("$datautf8[5]"),"\n", $q->end_td,"\n", $q->start_td({-align=>'left'}),"\n", $q->p("$datautf8[6]"),"\n", $q->end_td,"\n", $q->start_td({-align=>'left'}),"\n", $q->p("$datautf8[7]"),"\n", $q->end_td,"\n", $q->start_td({-align=>'left'}),"\n", $q->p("$datautf8[8]"),"\n", $q->end_td,"\n", $q->start_td({-align=>'left'}),"\n", $q->p("$datautf8[9]"),"\n", $q->end_td,"\n", $q->start_td({-align=>'left'}),"\n", $q->p("$datautf8[10]"),"\n", $q->end_td,"\n", $q->start_td({-align=>'left'}),"\n", $q->p("$datautf8[11]"),"\n", $q->end_td,"\n", $q->start_td({-align=>'left'}),"\n", $q->p("$datautf8[12]"),"\n", $q->end_td,"\n", $q->start_td({-align=>'left'}),"\n", $q->p("$datautf8[13]"),"\n", $q->end_td,"\n", $q->end_Tr,"\n\n"; } $q->end_table; print $q->end_html; $sth->finish; $dbh->disconnect; exit 0;

Replies are listed 'Best First'.
Re^5: DBI, and date type SQLite
by NetWallah (Abbot) on Oct 09, 2011 at 23:47 UTC
    Hola, Nando ..

    Disclaimer : I'm neither a DBA nor a DBI expert but...
    It seems to me that the binding for "ORDER BY xxx", the xxx needs to be a FIELD-NAME, not a value, so, the resolution of the variable at that location should be completed before the "prepare" is executed. In other words, I don't think that can be bound to a "?" for "execute-time" binding.

    Other style comments : (You did not ask, but this is a perl website, and sometimes we provide unsolicited advice that you are free to reject):

    In your table row filling code, you add "\n", which will be ignored in HTML. I would suggest:

    print $q->start_table({-border=>'1', -align=>'center',-cellpadding=>'6'} +),"\n\n"; while ( @data = $sth->fetchrow_array()){ print $q->start_Tr,"\n", # This '\n' is useful only in case someone read +s the HTML source map ({ td({-align=>'left'},decode("utf-8",$_) )."\n" } @data), $q->end_Tr,"\n"; } print $q->end_table,"\n";

    Another thing that would help is if you provided the SQL schema, including just a few records of your Sqlite db, so that we could recreate your problem.

    Also, passing along something I just learned:
    the parameter for 'julianday' is TEXT, not a 'DATE' type. Hopefully, your sqlite database has the fields defined as TEXT fields, and the bind parameter is also a date in text form.
    In my tests, the dash (-) separator works, but slash (/) does not.

                "XML is like violence: if it doesn't solve your problem, use more."

      I guess I was mistaken about the "ORDER BY ?" provided at run-time - it seems to work fine.

      The following code does deliver selected records:

                  "XML is like violence: if it doesn't solve your problem, use more."

        Using a placeholder within the ORDER BY clause happens to work with for example MySQL, but is not supported everywhere. Specifically when preparing a statement, the database driver must be enabled to create a query plan for the statement, whether placeholders are present or not. Placeholders for (e.g.) the table name or the order by clause prevent that.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://930506]
QM waves to the peanut gallery, and wishes he were writing Perl today.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2017-06-29 09:58 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (657 votes). Check out past polls.