my $dsn = "dbi:SQLite:dbname=:memory:"; # better in read/write operations performance than disk-file saved database.
my $user = "";
my $password = "";
my $dbh = DBI->connect($dsn, $user, $password,{});
#… Doing some processing on the database (Creating tables/Inserting rows/Updating fields)
#… After finishing, I need to save the database to a local disk file.
Run Code Online (Sandbox Code Playgroud)
我需要做的是在完成内存数据库的播放后,我需要将其保存到磁盘文件中file.db
.
更新(回答总结):
•有用的命令:感谢 Schwern的回答和评论.
$dbh->sqlite_backup_to_file( $file_path )
将数据库从内存复制到文件.$dbh->sqlite_backup_from_file( $file_path )
将数据库从文件复制到内存.my $dbh = DBI->connect($dsn, $user, $password,{AutoCommit => 0})
禁用AutoCommit似乎是一个更好,更简单的选项来优化性能,就像使用前两个命令一样.我只需要确保在关闭AutoCommit时,SQLite SELECT操作不会执行任何磁盘活动(其他问题).
是的,您可以$dbh->sqlite_backup_to_file( $filename )
像普通的SQLite数据库一样使用然后连接到该文件.有关更多信息,请参阅SQLite Backup API文档.
但是,通过关闭AutoCommit并在完成批量插入后仅提交事务,您可以以相同的性能完成相同的操作.SQLite可能会将所有插入内容保存在内存中,直到它们被提交为止.
my $dbh = DBI->connect(
"dbi:SQLite:dbname=test.sqlite", undef, undef, { RaiseError => 1, AutoCommit => 0 }
);
...do your inserts...
$dbh->commit;
Run Code Online (Sandbox Code Playgroud)
一个简单的基准测试表明这一点同样快,而且更灵活.
关闭AutoCommit将为您提供任何选项的巨大推动.