Efficient way to work with SQLite in Mojolicious::Lite

125 views
Skip to first unread message

khalil zakaria Zemmoura

unread,
Oct 1, 2016, 9:37:04 AM10/1/16
to Mojolicious
Hello,

I just started playing with mojolicious and i am trying to create a CRUD app for the learning purpose
the code i'll show is working fine but i suspect i am doing things in an non efficient way, plus, Morbo complain about "number od element in anonimous hash":

So here is the code i am using:

...

helper dbi => sub {
    my $dbi = DBIx::Custom->connect(dsn => "dbi:SQLite:dbname=database.db",
                                    option => {sqlite_unicode => 1});
};

get '/test' => sub {
  my $c = shift;
  my $result = $c->dbi->select(table => 'table');
  $result = $result->all;
  $c->stash(people => $result);
  $c->render(template => 'listing');
};

@@  listing.html.ep
% layout 'testing', title 'Listing';
...
    <table class="table table-striped">
     ...
        <tbody>
              % foreach my $user (@{$people}) {
                  <tr>
                    <td>
                        %= $user->{name}
                    </td>
                    <td>
                        %= $user->{town}
                    </td>
                    <td>
                        %= $user->{structure}
                    </td>
            % }
          </tr>
        </tbody>
    </table>

@@ layouts/testing.html.ep
...

the message that morbo displayed in the terminal is:

Odd number of elements in anonymous hash at /home/.../Mojo/Util.pm line 434.

Why this message?

In the documentation of the DBIx::Custom the code was:

use Mojolicious::Lite
    use DBIx::Custom;

    app->attr(
        dbi => sub {
            my $dbi = DBIx::Custom->connect(dsn => "...");
            return $dbi;
        }
    );

    get '/foo' => sub {
        my $self = shift;
        $self->dbi->select(...);
        $self->render;
    };

    app->start;

It's not working for me and if someone could explain me, that will be great.

I noticed this code is not using stash explicitly! so i suspect that i am doing something non optimal in my code.

I used stash because i don't know how to access data passed to templates another way. if there is another way to do it, let me know, please

thanks a lot.

Best regards
Zakaria

Pavel K

unread,
Oct 2, 2016, 1:32:13 PM10/2/16
to Mojolicious
Stash is a good practic. But I think you must use template variables, like this:
<% $user->{name} %> instead %= $user->{name}

khalil zakaria Zemmoura

unread,
Oct 2, 2016, 5:40:19 PM10/2/16
to Mojolicious
I think you meant <%= $user->{name} %> instead of <% $user->{name} %> because the last one throw an error.

Is there any technical difference between    %= $vat    and    <%= $var %>
thank you for your answer

Pavel K

unread,
Oct 3, 2016, 2:31:27 AM10/3/16
to Mojolicious
Yes, I mean <%= $user->{name} %> 

понедельник, 3 октября 2016 г., 0:40:19 UTC+3 пользователь khalil zakaria Zemmoura написал:

Paul Williams

unread,
Oct 5, 2016, 7:55:26 AM10/5/16
to Mojolicious

khalil zakaria Zemmoura

unread,
Oct 5, 2016, 9:57:01 AM10/5/16
to mojol...@googlegroups.com

Thanks, i have read this, but thought that there was a difference between them that wasn't mentioned.

Regards


--
You received this message because you are subscribed to a topic in the Google Groups "Mojolicious" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mojolicious/ud4mSc0kJ4s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mojolicious+unsubscribe@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Stefan Adams

unread,
Oct 5, 2016, 10:05:51 AM10/5/16
to mojolicious

On Oct 5, 2016 7:57 AM, "khalil zakaria Zemmoura" <zemmour...@gmail.com> wrote:
>
> Thanks, i have read this, but thought that there was a difference between them that wasn't mentioned.

You will soon come to realize the precision of the Mojo docs. There is nothing that isn't said that should be; nothing that shouldn't be said that is.

The Mojolicious docs are as perfect as Mojolicious itself.

Give or take a 1% margin of error, but I say this with all sincerity.

khalil zakaria Zemmoura

unread,
Oct 5, 2016, 3:12:22 PM10/5/16
to mojol...@googlegroups.com

I agree with you, it's very well documented but as a new user, i am not very familiar with the structure of the doc.

Some thoughts about the documentation:

It will be great I think if we had some how-to's that shows how easy is to build a real small app that interact with database and stuff like that (crud app) like Django does cause mojolicious is powerful and pleasant to use.

Showing that will attract more new devs as well as experimented devs that are looking for a neat API.

Of course they are examples in the wiki but looking to them require some knowledge of mojolicious

Regards


--

Dan Book

unread,
Oct 5, 2016, 4:29:33 PM10/5/16
to mojol...@googlegroups.com
The https://metacpan.org/pod/Mojo::SQLite and https://metacpan.org/pod/Mojo::Pg frameworks are built on DBI and are the usual way to connect to these databases from Mojolicious. Those distributions also come with a blog example in https://metacpan.org/source/DBOOK/Mojo-SQLite-1.000/examples/blog and https://metacpan.org/source/SRI/Mojo-Pg-2.30/examples/blog . Mojolicious does not have any built in database connectivity so any database examples would be in the separate distributions.

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+unsubscribe@googlegroups.com.

khalil zakaria Zemmoura

unread,
Oct 5, 2016, 5:36:53 PM10/5/16
to Mojolicious
thanks a lot,


Le mercredi 5 octobre 2016 21:29:33 UTC+1, Dan Book a écrit :
The https://metacpan.org/pod/Mojo::SQLite and https://metacpan.org/pod/Mojo::Pg frameworks are built on DBI and are the usual way to connect to these databases from Mojolicious. Those distributions also come with a blog example in https://metacpan.org/source/DBOOK/Mojo-SQLite-1.000/examples/blog and https://metacpan.org/source/SRI/Mojo-Pg-2.30/examples/blog . Mojolicious does not have any built in database connectivity so any database examples would be in the separate distributions.
On Wed, Oct 5, 2016 at 3:12 PM, khalil zakaria Zemmoura <zemmour...@gmail.com> wrote:

I agree with you, it's very well documented but as a new user, i am not very familiar with the structure of the doc.

Some thoughts about the documentation:

It will be great I think if we had some how-to's that shows how easy is to build a real small app that interact with database and stuff like that (crud app) like Django does cause mojolicious is powerful and pleasant to use.

Showing that will attract more new devs as well as experimented devs that are looking for a neat API.

Of course they are examples in the wiki but looking to them require some knowledge of mojolicious

Regards

Le 5 oct. 2016 15:05, "Stefan Adams" <s103...@gmail.com> a écrit :

On Oct 5, 2016 7:57 AM, "khalil zakaria Zemmoura" <zemmour...@gmail.com> wrote:
>
> Thanks, i have read this, but thought that there was a difference between them that wasn't mentioned.

You will soon come to realize the precision of the Mojo docs. There is nothing that isn't said that should be; nothing that shouldn't be said that is.

The Mojolicious docs are as perfect as Mojolicious itself.

Give or take a 1% margin of error, but I say this with all sincerity.

--
You received this message because you are subscribed to a topic in the Google Groups "Mojolicious" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mojolicious/ud4mSc0kJ4s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mojolicious...@googlegroups.com.

To post to this group, send email to mojol...@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages