0

I'm create database

sqlite3 database 
    create table if not exists entries (
        id integer primary key autoincrement,
        title string not null,
        text string not null
    );
    ^D

Where should i put this database ? After

  sub connect_db {
    my $dbh = DBI->connect("dbi:SQLite:dbname=".setting('database')) or
       die $DBI::errstr;

    return $dbh;
  }

  sub init_db {
    my $db = connect_db();
    my $schema = read_file('./schema.sql');
    $db->do($schema) or die $db->errstr;
  }

get '/' => sub {
    my $db = connect_db();
    my $sql = 'select id, title, text from entries order by id desc';
    my $sth = $db->prepare($sql) or die $db->errstr;
    $sth->execute or die $sth->errstr;
    template 'show_entries.tt', { 
       'msg' => get_flash(),
       'add_entry_url' => uri_for('/add'),
       'entries' => $sth->fetchall_hashref('id'),
    };
  };

Recieve an Error Runtime Error near "desk" : syntax error at /home/ultramozg/App/lib/App.pm line 40, line 16

What it's my mistake ?

Dima Candu
  • 25
  • 1
  • 6

2 Answers2

2

I highly recommend you use Dancer::Plugin::Database instead of the connect_db routine you're proposing. The way you're doing it will probably create leftover open connections and therefore all sorts of problems. Dancer::Plugin::Database handles persistent connections for you. Doc for plugin:

https://metacpan.org/pod/Dancer::Plugin::Database

Once you install Dancer::Plugin::Database and configure it in config.yml, then whenever you need to a database handle, you just:

my $dbh => database('my_database_name');

and don't bother disconnecting when done.

If you're using ubuntu, just:

apt-get install libdancer-plugin-database-perl

Good luck on your project!

sid_com
  • 24,137
  • 26
  • 96
  • 187
yahermann
  • 1,539
  • 1
  • 12
  • 33
0

You have a ^D character for starters - last line in the first section. Please learn to read output. It tells you line 16.

Check where the word "desk" appears in your code as well.

ddoor
  • 5,819
  • 9
  • 34
  • 41