【问题标题】:Dancer not save data in sqlite after reboot重新启动后舞者没有在sqlite中保存数据
【发布时间】:2013-09-27 10:27:05
【问题描述】:
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'),
       };
};

为什么重启后数据库中的系统条目没有保存?大小数据库不增加。 为什么会这样?

【问题讨论】:

  • 您的代码没有将任何项目保存到数据库中。
  • 您希望看到什么保存到数据库中?没有任何代码可以保存任何东西。 init_db() 子例程看起来像是用于设置数据库模式,但它不是从您向我们展示的任何代码中调用的。
  • init_db() 在代码末尾调用。怎么办?

标签: perl sqlite dbi dancer


【解决方案1】:

您可能正在使用的示例脚本https://github.com/PerlDancer/dancer-tutorial/blob/master/dancr.pl 正在默认临时目录中显式创建数据库文件:

set 'database' => File::Spec->catfile(File::Spec->tmpdir(), 'dancr.db');

可能您的系统配置为在重新启动后清理 /tmp(或任何临时目录),或者 /tmp 位于内存文件系统上。

如果你想要一个持久的数据库,那么改变数据库的位置。

【讨论】:

  • 然后我有一个问题,为什么要创建一个数据库使用这个代码 $ cat - | sqlite3数据库如果不存在条目则创建表(id整数主键自动增量,标题字符串不为空,文本字符串不为空); ^D
  • 这是一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-08
  • 2015-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多